Skip to content

Commit cc8d12a

Browse files
authored
Changes on 'minItems' and 'maxItems' not detected (#753)
1 parent 771a642 commit cc8d12a

File tree

7 files changed

+244
-1
lines changed

7 files changed

+244
-1
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.openapitools.openapidiff.core.model.deferred.DeferredChanged;
2020
import org.openapitools.openapidiff.core.model.deferred.RecursiveSchemaSet;
2121
import org.openapitools.openapidiff.core.model.schema.*;
22+
import org.openapitools.openapidiff.core.model.schema.ChangedMaxItems;
23+
import org.openapitools.openapidiff.core.model.schema.ChangedMinItems;
2224

2325
public class SchemaDiffResult {
2426
protected ChangedSchema changedSchema;
@@ -74,7 +76,9 @@ public <V extends Schema<X>, X> DeferredChanged<ChangedSchema> diff(
7476
.setMultipleOf(new ChangedMultipleOf(left.getMultipleOf(), right.getMultipleOf()))
7577
.setNullable(new ChangedNullable(left.getNullable(), right.getNullable()))
7678
.setExamples(new ChangedExamples(left.getExamples(), right.getExamples()))
77-
.setExample(new ChangedExample(left.getExample(), right.getExample()));
79+
.setExample(new ChangedExample(left.getExample(), right.getExample()))
80+
.setMaxItems(new ChangedMaxItems(left.getMaxItems(), right.getMaxItems(), context))
81+
.setMinItems(new ChangedMinItems(left.getMinItems(), right.getMinItems(), context));
7882
builder
7983
.with(
8084
openApiDiff

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

+34
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.util.stream.Collectors;
1010
import java.util.stream.Stream;
1111
import org.openapitools.openapidiff.core.model.schema.ChangedEnum;
12+
import org.openapitools.openapidiff.core.model.schema.ChangedMaxItems;
1213
import org.openapitools.openapidiff.core.model.schema.ChangedMaxLength;
14+
import org.openapitools.openapidiff.core.model.schema.ChangedMinItems;
1315
import org.openapitools.openapidiff.core.model.schema.ChangedMultipleOf;
1416
import org.openapitools.openapidiff.core.model.schema.ChangedNullable;
1517
import org.openapitools.openapidiff.core.model.schema.ChangedNumericRange;
@@ -41,6 +43,8 @@ public class ChangedSchema implements ComposedChanged {
4143
protected ChangedMaxLength maxLength;
4244
protected ChangedNumericRange numericRange;
4345
protected ChangedMultipleOf multipleOf;
46+
protected ChangedMaxItems maxItems;
47+
protected ChangedMinItems minItems;
4448
protected ChangedNullable nullable;
4549
protected boolean discriminatorPropertyChanged;
4650
protected ChangedSchema items;
@@ -125,6 +129,8 @@ public List<Changed> getChangedElements() {
125129
maxLength,
126130
numericRange,
127131
multipleOf,
132+
maxItems,
133+
minItems,
128134
nullable,
129135
extensions))
130136
.collect(Collectors.toList());
@@ -289,6 +295,14 @@ public ChangedMultipleOf getMultipleOf() {
289295
return this.multipleOf;
290296
}
291297

298+
public ChangedMaxItems getMaxItems() {
299+
return this.maxItems;
300+
}
301+
302+
public ChangedMinItems getMinItems() {
303+
return this.minItems;
304+
}
305+
292306
public ChangedNullable getNullable() {
293307
return this.nullable;
294308
}
@@ -441,6 +455,18 @@ public ChangedSchema setMultipleOf(final ChangedMultipleOf multipleOf) {
441455
return this;
442456
}
443457

458+
public ChangedSchema setMaxItems(final ChangedMaxItems maxItems) {
459+
clearChangedCache();
460+
this.maxItems = maxItems;
461+
return this;
462+
}
463+
464+
public ChangedSchema setMinItems(final ChangedMinItems minItems) {
465+
clearChangedCache();
466+
this.minItems = minItems;
467+
return this;
468+
}
469+
444470
public ChangedSchema setNullable(final ChangedNullable nullable) {
445471
clearChangedCache();
446472
this.nullable = nullable;
@@ -505,6 +531,8 @@ public boolean equals(Object o) {
505531
&& Objects.equals(maxLength, that.maxLength)
506532
&& Objects.equals(numericRange, that.numericRange)
507533
&& Objects.equals(multipleOf, that.multipleOf)
534+
&& Objects.equals(maxItems, that.maxItems)
535+
&& Objects.equals(minItems, that.minItems)
508536
&& Objects.equals(nullable, that.nullable)
509537
&& Objects.equals(items, that.items)
510538
&& Objects.equals(oneOfSchema, that.oneOfSchema)
@@ -537,6 +565,8 @@ public int hashCode() {
537565
maxLength,
538566
numericRange,
539567
multipleOf,
568+
maxItems,
569+
minItems,
540570
nullable,
541571
discriminatorPropertyChanged,
542572
items,
@@ -591,6 +621,10 @@ public java.lang.String toString() {
591621
+ this.getNumericRange()
592622
+ ", multipleOf="
593623
+ this.getMultipleOf()
624+
+ ", maxItems="
625+
+ this.getMaxItems()
626+
+ ", minItems="
627+
+ this.getMinItems()
594628
+ ", nullable="
595629
+ this.getNullable()
596630
+ ", discriminatorPropertyChanged="
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 ChangedMaxItems implements Changed {
8+
private final Integer oldValue;
9+
private final Integer newValue;
10+
private final DiffContext context;
11+
12+
public ChangedMaxItems(Integer oldValue, Integer newValue, DiffContext context) {
13+
this.oldValue = oldValue;
14+
this.newValue = newValue;
15+
this.context = context;
16+
}
17+
18+
@Override
19+
public DiffResult isChanged() {
20+
if (oldValue == null && newValue == null) {
21+
return DiffResult.NO_CHANGES;
22+
}
23+
if (oldValue == null || newValue == null) {
24+
return DiffResult.COMPATIBLE;
25+
}
26+
if (newValue < oldValue) {
27+
return DiffResult.INCOMPATIBLE;
28+
}
29+
return DiffResult.COMPATIBLE;
30+
}
31+
32+
public Integer getOldValue() {
33+
return oldValue;
34+
}
35+
36+
public Integer getNewValue() {
37+
return newValue;
38+
}
39+
40+
public DiffContext getContext() {
41+
return context;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 ChangedMinItems implements Changed {
8+
private final Integer oldValue;
9+
private final Integer newValue;
10+
private final DiffContext context;
11+
12+
public ChangedMinItems(Integer oldValue, Integer newValue, DiffContext context) {
13+
this.oldValue = oldValue;
14+
this.newValue = newValue;
15+
this.context = context;
16+
}
17+
18+
@Override
19+
public DiffResult isChanged() {
20+
if (oldValue == null && newValue == null) {
21+
return DiffResult.NO_CHANGES;
22+
}
23+
if (oldValue == null || newValue == null) {
24+
return DiffResult.COMPATIBLE;
25+
}
26+
if (newValue > oldValue) {
27+
return DiffResult.INCOMPATIBLE;
28+
}
29+
return DiffResult.COMPATIBLE;
30+
}
31+
32+
public Integer getOldValue() {
33+
return oldValue;
34+
}
35+
36+
public Integer getNewValue() {
37+
return newValue;
38+
}
39+
40+
public DiffContext getContext() {
41+
return context;
42+
}
43+
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,41 @@ public void changeMultipleOfHandling() {
135135
assertThat(props.get("field4").getMultipleOf().getRight()).isNull();
136136
}
137137

138+
@Test // issues #480
139+
public void changeMinMaxItemsHandling() {
140+
ChangedOpenApi changedOpenApi =
141+
OpenApiCompare.fromLocations(
142+
"schemaDiff/schema-min-max-items-diff-1.yaml",
143+
"schemaDiff/schema-min-max-items-diff-2.yaml");
144+
ChangedSchema changedSchema =
145+
getRequestBodyChangedSchema(
146+
changedOpenApi, POST, "/schema/array/items", "application/json");
147+
148+
assertThat(changedSchema).isNotNull();
149+
Map<String, ChangedSchema> props = changedSchema.getChangedProperties();
150+
assertThat(props).isNotEmpty();
151+
152+
// Check increasing of minItems
153+
assertThat(props.get("field1").getMinItems().isIncompatible()).isTrue();
154+
assertThat(props.get("field1").getMinItems().getOldValue()).isEqualTo(1);
155+
assertThat(props.get("field1").getMinItems().getNewValue()).isEqualTo(2);
156+
157+
// Check decreasing of minItems
158+
assertThat(props.get("field2").getMinItems().isCompatible()).isTrue();
159+
assertThat(props.get("field2").getMinItems().getOldValue()).isEqualTo(20);
160+
assertThat(props.get("field2").getMinItems().getNewValue()).isEqualTo(10);
161+
162+
// Check increasing of maxItems
163+
assertThat(props.get("field3").getMaxItems().isCompatible()).isTrue();
164+
assertThat(props.get("field3").getMaxItems().getOldValue()).isEqualTo(90);
165+
assertThat(props.get("field3").getMaxItems().getNewValue()).isEqualTo(100);
166+
167+
// Check decreasing of maxItems
168+
assertThat(props.get("field4").getMaxItems().isIncompatible()).isTrue();
169+
assertThat(props.get("field4").getMaxItems().getOldValue()).isEqualTo(100);
170+
assertThat(props.get("field4").getMaxItems().getNewValue()).isEqualTo(90);
171+
}
172+
138173
@Test // issue #482
139174
public void changeNullabeHandling() {
140175
ChangedOpenApi changedOpenApi =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.1.0
2+
info:
3+
description: Schema diff
4+
title: schema diff
5+
version: 1.0.0
6+
paths:
7+
/schema/array/items:
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: array
21+
items:
22+
type: string
23+
minItems: 1
24+
maxItems: 100
25+
field2:
26+
type: array
27+
items:
28+
type: string
29+
minItems: 20
30+
maxItems: 100
31+
field3:
32+
type: array
33+
items:
34+
type: string
35+
minItems: 1
36+
maxItems: 90
37+
field4:
38+
type: array
39+
items:
40+
type: string
41+
minItems: 1
42+
maxItems: 100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.1.0
2+
info:
3+
description: Schema diff
4+
title: schema diff
5+
version: 1.0.0
6+
paths:
7+
/schema/array/items:
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: array
21+
items:
22+
type: string
23+
minItems: 2
24+
maxItems: 100
25+
field2:
26+
type: array
27+
items:
28+
type: string
29+
minItems: 10
30+
maxItems: 100
31+
field3:
32+
type: array
33+
items:
34+
type: string
35+
minItems: 1
36+
maxItems: 100
37+
field4:
38+
type: array
39+
items:
40+
type: string
41+
minItems: 1
42+
maxItems: 90

0 commit comments

Comments
 (0)