From 58c1d9747136d5afa26054d20b2876e10d40850f Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Sun, 15 Nov 2026 15:29:40 +0300 Subject: [PATCH] fix: treat a required request body becoming optional as compatible What: - a request body changing from required to optional is now reported as a compatible change instead of an incompatible one - optional -> required stays incompatible; the check is now gated by the previously unused incompatible.request.body.increased property (REQUEST_BODY_REQUIRED_INCREASED) instead of incompatible.openapi.endpoints.decreased - a removed request body whose old body was required stays incompatible Why: - existing clients that keep sending the body still work when the body becomes optional, so the change cannot break them (Fixes #329) How: - ChangedRequestBody.isCoreChanged short-circuits to COMPATIBLE when both bodies exist and only the required flag decreases, mirroring the directional checks in ChangedRequired and ChangedReadOnly - RequestBodyBCTest now covers both directions and RequestDiffTest gains model-level assertions for both directions Signed-off-by: Inf1mum --- .../core/model/ChangedRequestBody.java | 17 +++++++-- .../openapidiff/core/RequestDiffTest.java | 36 +++++++++++++++++++ .../core/backcompat/RequestBodyBCTest.java | 15 ++++++-- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/openapitools/openapidiff/core/model/ChangedRequestBody.java b/core/src/main/java/org/openapitools/openapidiff/core/model/ChangedRequestBody.java index 48cc4cf0e..af088cdc8 100644 --- a/core/src/main/java/org/openapitools/openapidiff/core/model/ChangedRequestBody.java +++ b/core/src/main/java/org/openapitools/openapidiff/core/model/ChangedRequestBody.java @@ -1,6 +1,6 @@ package org.openapitools.openapidiff.core.model; -import static org.openapitools.openapidiff.core.model.BackwardIncompatibleProp.OPENAPI_ENDPOINTS_DECREASED; +import static org.openapitools.openapidiff.core.model.BackwardIncompatibleProp.REQUEST_BODY_REQUIRED_INCREASED; import io.swagger.v3.oas.models.parameters.RequestBody; import java.util.Arrays; @@ -33,12 +33,25 @@ public DiffResult isCoreChanged() { if (!changeRequired) { return DiffResult.NO_CHANGES; } - if (OPENAPI_ENDPOINTS_DECREASED.enabled(context)) { + // Making a required request body optional is backward compatible: existing + // clients that keep sending the body still work (issue #329). + if (isRequestRequiredDecreased()) { + return DiffResult.COMPATIBLE; + } + if (REQUEST_BODY_REQUIRED_INCREASED.enabled(context)) { return DiffResult.INCOMPATIBLE; } return DiffResult.COMPATIBLE; } + private boolean isRequestRequiredDecreased() { + // Both bodies must exist: a removed required body stays incompatible. + return oldRequestBody != null + && newRequestBody != null + && Boolean.TRUE.equals(oldRequestBody.getRequired()) + && !Boolean.TRUE.equals(newRequestBody.getRequired()); + } + public RequestBody getOldRequestBody() { return this.oldRequestBody; } diff --git a/core/src/test/java/org/openapitools/openapidiff/core/RequestDiffTest.java b/core/src/test/java/org/openapitools/openapidiff/core/RequestDiffTest.java index 31b76f760..88a517bde 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/RequestDiffTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/RequestDiffTest.java @@ -1,8 +1,11 @@ package org.openapitools.openapidiff.core; +import static org.assertj.core.api.Assertions.assertThat; import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiChangedEndpoints; import org.junit.jupiter.api.Test; +import org.openapitools.openapidiff.core.model.ChangedOpenApi; +import org.openapitools.openapidiff.core.model.ChangedOperation; public class RequestDiffTest { private final String OPENAPI_DOC1 = "request_diff_1.yaml"; @@ -12,4 +15,37 @@ public class RequestDiffTest { public void testDiffDifferent() { assertOpenApiChangedEndpoints(OPENAPI_DOC1, OPENAPI_DOC2); } + + @Test + public void testRequestBodyRequiredDecreasedIsCompatible() { + // required -> optional: existing clients that keep sending the body still + // work (issue #329) + ChangedOpenApi changedOpenApi = + OpenApiCompare.fromLocations( + "bc_request_body_required_changed.yaml", "bc_request_body_base.yaml"); + assertThat(changedOpenApi.getChangedOperations()).hasSize(1); + ChangedOperation operation = changedOpenApi.getChangedOperations().get(0); + assertThat(operation.getRequestBody()).isNotNull(); + assertThat(operation.getRequestBody().isChangeRequired()).isTrue(); + assertThat(operation.getRequestBody().isChanged().isCompatible()).isTrue(); + assertThat(operation.isCompatible()).isTrue(); + assertThat(changedOpenApi.isChanged().isDifferent()).isTrue(); + assertThat(changedOpenApi.isChanged().isCompatible()).isTrue(); + assertThat(changedOpenApi.isChanged().isIncompatible()).isFalse(); + } + + @Test + public void testRequestBodyRequiredIncreasedIsIncompatible() { + // optional -> required: clients that do not send the body break + ChangedOpenApi changedOpenApi = + OpenApiCompare.fromLocations( + "bc_request_body_base.yaml", "bc_request_body_required_changed.yaml"); + assertThat(changedOpenApi.getChangedOperations()).hasSize(1); + ChangedOperation operation = changedOpenApi.getChangedOperations().get(0); + assertThat(operation.getRequestBody()).isNotNull(); + assertThat(operation.getRequestBody().isChangeRequired()).isTrue(); + assertThat(operation.getRequestBody().isChanged().isIncompatible()).isTrue(); + assertThat(operation.isCompatible()).isFalse(); + assertThat(changedOpenApi.isChanged().isIncompatible()).isTrue(); + } } diff --git a/core/src/test/java/org/openapitools/openapidiff/core/backcompat/RequestBodyBCTest.java b/core/src/test/java/org/openapitools/openapidiff/core/backcompat/RequestBodyBCTest.java index 5b1000702..a3b2761c2 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/backcompat/RequestBodyBCTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/backcompat/RequestBodyBCTest.java @@ -1,8 +1,9 @@ package org.openapitools.openapidiff.core.backcompat; +import static org.openapitools.openapidiff.core.TestUtils.assertSpecChangedButCompatible; import static org.openapitools.openapidiff.core.TestUtils.assertSpecIncompatible; import static org.openapitools.openapidiff.core.TestUtils.assertSpecUnchanged; -import static org.openapitools.openapidiff.core.model.BackwardIncompatibleProp.OPENAPI_ENDPOINTS_DECREASED; +import static org.openapitools.openapidiff.core.model.BackwardIncompatibleProp.REQUEST_BODY_REQUIRED_INCREASED; import org.junit.jupiter.api.Test; import org.openapitools.openapidiff.core.model.BackwardIncompatibleProp; @@ -16,8 +17,16 @@ public void unchanged() { } @Test - public void requiredChanged() { - BackwardIncompatibleProp prop = OPENAPI_ENDPOINTS_DECREASED; + public void requiredIncreased() { + // optional -> required: clients that do not send the body break + BackwardIncompatibleProp prop = REQUEST_BODY_REQUIRED_INCREASED; assertSpecIncompatible(BASE, "bc_request_body_required_changed.yaml", prop); } + + @Test + public void requiredDecreasedButCompatible() { + // required -> optional: existing clients that keep sending the body still + // work (issue #329) + assertSpecChangedButCompatible("bc_request_body_required_changed.yaml", BASE); + } }