Skip to content

Commit 30a6d78

Browse files
authored
Changes on "minProperties" / "maxProperties" are not detected (#754)
Fixes #479
1 parent cc8d12a commit 30a6d78

File tree

7 files changed

+228
-3
lines changed

7 files changed

+228
-3
lines changed

core/src/main/java/org/openapitools/openapidiff/core/compare/schemadiffresult/SchemaDiffResult.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ public <V extends Schema<X>, X> DeferredChanged<ChangedSchema> diff(
7878
.setExamples(new ChangedExamples(left.getExamples(), right.getExamples()))
7979
.setExample(new ChangedExample(left.getExample(), right.getExample()))
8080
.setMaxItems(new ChangedMaxItems(left.getMaxItems(), right.getMaxItems(), context))
81-
.setMinItems(new ChangedMinItems(left.getMinItems(), right.getMinItems(), context));
81+
.setMinItems(new ChangedMinItems(left.getMinItems(), right.getMinItems(), context))
82+
.setMaxProperties(
83+
new ChangedMaxProperties(left.getMaxProperties(), right.getMaxProperties(), context))
84+
.setMinProperties(
85+
new ChangedMinProperties(left.getMinProperties(), right.getMinProperties(), context));
8286
builder
8387
.with(
8488
openApiDiff

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

+36-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import org.openapitools.openapidiff.core.model.schema.ChangedEnum;
1212
import org.openapitools.openapidiff.core.model.schema.ChangedMaxItems;
1313
import org.openapitools.openapidiff.core.model.schema.ChangedMaxLength;
14+
import org.openapitools.openapidiff.core.model.schema.ChangedMaxProperties;
1415
import org.openapitools.openapidiff.core.model.schema.ChangedMinItems;
16+
import org.openapitools.openapidiff.core.model.schema.ChangedMinProperties;
1517
import org.openapitools.openapidiff.core.model.schema.ChangedMultipleOf;
1618
import org.openapitools.openapidiff.core.model.schema.ChangedNullable;
1719
import org.openapitools.openapidiff.core.model.schema.ChangedNumericRange;
@@ -45,6 +47,8 @@ public class ChangedSchema implements ComposedChanged {
4547
protected ChangedMultipleOf multipleOf;
4648
protected ChangedMaxItems maxItems;
4749
protected ChangedMinItems minItems;
50+
protected ChangedMaxProperties maxProperties;
51+
protected ChangedMinProperties minProperties;
4852
protected ChangedNullable nullable;
4953
protected boolean discriminatorPropertyChanged;
5054
protected ChangedSchema items;
@@ -131,6 +135,8 @@ public List<Changed> getChangedElements() {
131135
multipleOf,
132136
maxItems,
133137
minItems,
138+
maxProperties,
139+
minProperties,
134140
nullable,
135141
extensions))
136142
.collect(Collectors.toList());
@@ -327,6 +333,14 @@ public ChangedExtensions getExtensions() {
327333
return this.extensions;
328334
}
329335

336+
public ChangedMaxProperties getMaxProperties() {
337+
return this.maxProperties;
338+
}
339+
340+
public ChangedMinProperties getMinProperties() {
341+
return this.minProperties;
342+
}
343+
330344
public ChangedSchema setContext(final DiffContext context) {
331345
this.context = context;
332346
return this;
@@ -503,6 +517,18 @@ public ChangedSchema setExtensions(final ChangedExtensions extensions) {
503517
return this;
504518
}
505519

520+
public ChangedSchema setMaxProperties(final ChangedMaxProperties maxProperties) {
521+
clearChangedCache();
522+
this.maxProperties = maxProperties;
523+
return this;
524+
}
525+
526+
public ChangedSchema setMinProperties(final ChangedMinProperties minProperties) {
527+
clearChangedCache();
528+
this.minProperties = minProperties;
529+
return this;
530+
}
531+
506532
@Override
507533
public boolean equals(Object o) {
508534
if (this == o) return true;
@@ -537,7 +563,9 @@ public boolean equals(Object o) {
537563
&& Objects.equals(items, that.items)
538564
&& Objects.equals(oneOfSchema, that.oneOfSchema)
539565
&& Objects.equals(addProp, that.addProp)
540-
&& Objects.equals(extensions, that.extensions);
566+
&& Objects.equals(extensions, that.extensions)
567+
&& Objects.equals(maxProperties, that.maxProperties)
568+
&& Objects.equals(minProperties, that.minProperties);
541569
}
542570

543571
@Override
@@ -572,7 +600,9 @@ public int hashCode() {
572600
items,
573601
oneOfSchema,
574602
addProp,
575-
extensions);
603+
extensions,
604+
maxProperties,
605+
minProperties);
576606
}
577607

578608
@java.lang.Override
@@ -637,6 +667,10 @@ public java.lang.String toString() {
637667
+ this.getAddProp()
638668
+ ", extensions="
639669
+ this.getExtensions()
670+
+ ", maxProperties="
671+
+ this.getMaxProperties()
672+
+ ", minProperties="
673+
+ this.getMinProperties()
640674
+ ")";
641675
}
642676
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.openapitools.openapidiff.core.model.schema;
2+
3+
import org.openapitools.openapidiff.core.model.Changed;
4+
import org.openapitools.openapidiff.core.model.DiffContext;
5+
import org.openapitools.openapidiff.core.model.DiffResult;
6+
7+
public class ChangedMaxProperties implements Changed {
8+
private Integer oldValue;
9+
private Integer newValue;
10+
private DiffContext context;
11+
12+
public ChangedMaxProperties(Integer oldValue, Integer newValue, DiffContext context) {
13+
this.oldValue = oldValue;
14+
this.newValue = newValue;
15+
this.context = context;
16+
}
17+
18+
public Integer getOldValue() {
19+
return oldValue;
20+
}
21+
22+
public Integer getNewValue() {
23+
return newValue;
24+
}
25+
26+
@Override
27+
public DiffResult isChanged() {
28+
if (oldValue == null && newValue == null) {
29+
return DiffResult.NO_CHANGES;
30+
}
31+
32+
if (oldValue == null) {
33+
return DiffResult.INCOMPATIBLE;
34+
}
35+
36+
if (newValue == null) {
37+
return DiffResult.COMPATIBLE;
38+
}
39+
40+
if (!oldValue.equals(newValue)) {
41+
return oldValue > newValue ? DiffResult.INCOMPATIBLE : DiffResult.COMPATIBLE;
42+
}
43+
44+
return DiffResult.NO_CHANGES;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.openapitools.openapidiff.core.model.schema;
2+
3+
import org.openapitools.openapidiff.core.model.Changed;
4+
import org.openapitools.openapidiff.core.model.DiffContext;
5+
import org.openapitools.openapidiff.core.model.DiffResult;
6+
7+
public class ChangedMinProperties implements Changed {
8+
private Integer oldValue;
9+
private Integer newValue;
10+
private DiffContext context;
11+
12+
public ChangedMinProperties(Integer oldValue, Integer newValue, DiffContext context) {
13+
this.oldValue = oldValue;
14+
this.newValue = newValue;
15+
this.context = context;
16+
}
17+
18+
public Integer getOldValue() {
19+
return oldValue;
20+
}
21+
22+
public Integer getNewValue() {
23+
return newValue;
24+
}
25+
26+
@Override
27+
public DiffResult isChanged() {
28+
if (oldValue == null && newValue == null) {
29+
return DiffResult.NO_CHANGES;
30+
}
31+
32+
if (oldValue == null) {
33+
return DiffResult.INCOMPATIBLE;
34+
}
35+
36+
if (newValue == null) {
37+
return DiffResult.COMPATIBLE;
38+
}
39+
40+
if (!oldValue.equals(newValue)) {
41+
return newValue > oldValue ? DiffResult.INCOMPATIBLE : DiffResult.COMPATIBLE;
42+
}
43+
44+
return DiffResult.NO_CHANGES;
45+
}
46+
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,39 @@ public void changeNullabeHandling() {
200200
assertThat(props.get("field3").getNullable().getLeft()).isNull();
201201
assertThat(props.get("field3").getNullable().getRight()).isTrue();
202202
}
203+
204+
@Test // issue #479
205+
public void changeMinMaxPropertiesHandling() {
206+
ChangedOpenApi changedOpenApi =
207+
OpenApiCompare.fromLocations(
208+
"schemaDiff/schema-min-max-properties-diff-1.yaml",
209+
"schemaDiff/schema-min-max-properties-diff-2.yaml");
210+
ChangedSchema changedSchema =
211+
getRequestBodyChangedSchema(
212+
changedOpenApi, POST, "/schema/object/min-max-properties", "application/json");
213+
214+
assertThat(changedSchema).isNotNull();
215+
Map<String, ChangedSchema> props = changedSchema.getChangedProperties();
216+
assertThat(props).isNotEmpty();
217+
218+
// Check increasing of minProperties
219+
assertThat(props.get("field1").getMinProperties().isIncompatible()).isTrue();
220+
assertThat(props.get("field1").getMinProperties().getOldValue()).isEqualTo(1);
221+
assertThat(props.get("field1").getMinProperties().getNewValue()).isEqualTo(10);
222+
223+
// Check decreasing of minProperties
224+
assertThat(props.get("field2").getMinProperties().isCompatible()).isTrue();
225+
assertThat(props.get("field2").getMinProperties().getOldValue()).isEqualTo(10);
226+
assertThat(props.get("field2").getMinProperties().getNewValue()).isEqualTo(1);
227+
228+
// Check increasing of maxProperties
229+
assertThat(props.get("field3").getMaxProperties().isCompatible()).isTrue();
230+
assertThat(props.get("field3").getMaxProperties().getOldValue()).isEqualTo(10);
231+
assertThat(props.get("field3").getMaxProperties().getNewValue()).isEqualTo(100);
232+
233+
// Check decreasing of maxProperties
234+
assertThat(props.get("field4").getMaxProperties().isIncompatible()).isTrue();
235+
assertThat(props.get("field4").getMaxProperties().getOldValue()).isEqualTo(100);
236+
assertThat(props.get("field4").getMaxProperties().getNewValue()).isEqualTo(10);
237+
}
203238
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
openapi: 3.0.1
2+
info:
3+
description: Schema diff
4+
title: schema diff
5+
version: 1.0.0
6+
paths:
7+
/schema/object/min-max-properties:
8+
post:
9+
requestBody:
10+
content:
11+
application/json:
12+
schema:
13+
$ref: '#/components/schemas/TestDTO'
14+
components:
15+
schemas:
16+
TestDTO:
17+
type: object
18+
properties:
19+
field1:
20+
type: object
21+
minProperties: 1
22+
field2:
23+
type: object
24+
minProperties: 10
25+
field3:
26+
type: object
27+
maxProperties: 10
28+
field4:
29+
type: object
30+
maxProperties: 100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
openapi: 3.0.1
2+
info:
3+
description: Schema diff
4+
title: schema diff
5+
version: 1.0.0
6+
paths:
7+
/schema/object/min-max-properties:
8+
post:
9+
requestBody:
10+
content:
11+
application/json:
12+
schema:
13+
$ref: '#/components/schemas/TestDTO'
14+
components:
15+
schemas:
16+
TestDTO:
17+
type: object
18+
properties:
19+
field1:
20+
type: object
21+
minProperties: 10
22+
field2:
23+
type: object
24+
minProperties: 1
25+
field3:
26+
type: object
27+
maxProperties: 100
28+
field4:
29+
type: object
30+
maxProperties: 10

0 commit comments

Comments
 (0)