Skip to content

Commit 46c7f23

Browse files
Fix: False positive breaking change when removing optional field from response (#327)
1 parent 8d804d4 commit 46c7f23

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

core/src/main/java/org/openapitools/openapidiff/core/model/ChangedSchema.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ private DiffResult calculateCoreChanged() {
137137
&& !discriminatorPropertyChanged) {
138138
return DiffResult.NO_CHANGES;
139139
}
140+
boolean missingRequiredProperties =
141+
oldSchema != null
142+
&& oldSchema.getRequired() != null
143+
&& missingProperties.keySet().stream()
144+
.anyMatch(missingProperty -> oldSchema.getRequired().contains(missingProperty));
140145
boolean compatibleForResponse =
141-
missingProperties.isEmpty() && (oldSchema == null || newSchema != null);
146+
!missingRequiredProperties && (oldSchema == null || newSchema != null);
142147
if ((context.isRequest() && compatibleForRequest()
143148
|| context.isResponse() && compatibleForResponse)
144149
&& !changedType

core/src/test/java/org/openapitools/openapidiff/core/AddPropDiffTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.openapitools.openapidiff.core;
22

33
import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiAreEquals;
4-
import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiBackwardIncompatible;
4+
import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiBackwardCompatible;
55

66
import org.junit.jupiter.api.Test;
77

@@ -16,6 +16,6 @@ public void testDiffSame() {
1616

1717
@Test
1818
public void testDiffDifferent() {
19-
assertOpenApiBackwardIncompatible(OPENAPI_DOC1, OPENAPI_DOC2);
19+
assertOpenApiBackwardCompatible(OPENAPI_DOC1, OPENAPI_DOC2, true);
2020
}
2121
}

core/src/test/resources/recursive_model_1.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ components:
1919
schemas:
2020
B:
2121
type: object
22+
required: ["message2"]
2223
properties:
2324
message:
2425
type: string
@@ -27,4 +28,4 @@ components:
2728
details:
2829
type: array
2930
items:
30-
$ref: '#/components/schemas/B'
31+
$ref: '#/components/schemas/B'

0 commit comments

Comments
 (0)