Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -12,4 +15,51 @@ 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() {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
// 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();
}

@Test
public void testRequestBodyRemovalIsIncompatible() {
// required -> absent: clients that keep sending the required body break
ChangedOpenApi changedOpenApi =
OpenApiCompare.fromLocations(
"bc_request_body_required_deleted_1.yaml", "bc_request_body_required_deleted_2.yaml");
assertThat(changedOpenApi.getChangedOperations()).hasSize(1);
ChangedOperation operation = changedOpenApi.getChangedOperations().get(0);
assertThat(operation.isCompatible()).isFalse();
assertThat(operation.getRequestBody()).isNotNull();
assertThat(operation.getRequestBody().isChanged().isIncompatible()).isTrue();
assertThat(changedOpenApi.isChanged().isIncompatible()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
22 changes: 22 additions & 0 deletions core/src/test/resources/bc_request_body_required_deleted_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.0.0
info:
description: myDesc
title: myTitle
version: 1.0.0
paths:
/widgets:
post:
operationId: widgetCreate
requestBody:
required: true
content:
application/json:
schema:
type: string
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: string
16 changes: 16 additions & 0 deletions core/src/test/resources/bc_request_body_required_deleted_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.0
info:
description: myDesc
title: myTitle
version: 1.0.0
paths:
/widgets:
post:
operationId: widgetCreate
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: string