Skip to content

Commit 6c2fc5b

Browse files
committed
Fix problems when schema of type array
1 parent f123033 commit 6c2fc5b

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

src/main/java/com/qdesrame/openapi/diff/compare/schemadiffresult/ArraySchemaDiffResult.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public ArraySchemaDiffResult(OpenApiDiff openApiDiff) {
2222
public Optional<ChangedSchema> diff(HashSet<String> refSet, Components leftComponents, Components rightComponents, Schema left, Schema right, DiffContext context) {
2323
ArraySchema leftArraySchema = (ArraySchema) left;
2424
ArraySchema rightArraySchema = (ArraySchema) right;
25-
return openApiDiff.getSchemaDiff().diff(refSet, leftArraySchema.getItems(), rightArraySchema.getItems(), context.copyWithRequired(true));
25+
super.diff(refSet, leftComponents, rightComponents, left, right, context);
26+
openApiDiff.getSchemaDiff().diff(refSet, leftArraySchema.getItems(), rightArraySchema.getItems(), context.copyWithRequired(true))
27+
.ifPresent(changedSchema::setChangedItems);
28+
return isApplicable(context);
2629
}
2730
}

src/main/java/com/qdesrame/openapi/diff/compare/schemadiffresult/SchemaDiffResult.java

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public Optional<ChangedSchema> diff(HashSet<String> refSet, Components leftCompo
6262

6363
changedSchema.getIncreasedProperties().putAll(filterProperties(propertyDiff.getIncreased(), context));
6464
changedSchema.getMissingProperties().putAll(filterProperties(propertyDiff.getMissing(), context));
65+
return isApplicable(context);
66+
}
67+
68+
protected Optional<ChangedSchema> isApplicable(DiffContext context) {
69+
if (changedSchema.getChangedReadOnly().isUnchanged() && changedSchema.getChangedWriteOnly().isUnchanged()
70+
&& !isPropertyApplicable(changedSchema.getNewSchema(), context)) {
71+
return Optional.empty();
72+
}
6573
return isChanged(changedSchema);
6674
}
6775

src/main/java/com/qdesrame/openapi/diff/model/ChangedSchema.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class ChangedSchema implements Changed {
3737
protected boolean changedType;
3838
protected boolean changedMaxLength;
3939
protected boolean discriminatorPropertyChanged;
40+
protected ChangedSchema changedItems;
4041
protected ChangedOneOfSchema changedOneOfSchema;
4142
protected ChangedSchema addPropChangedSchema;
4243
protected ChangedExtensions changedExtensions;
@@ -56,7 +57,7 @@ public DiffResult isChanged() {
5657
&& changedProperties.values().size() == 0 && !changeDeprecated
5758
&& (changeRequired == null || changeRequired.isUnchanged()) && !discriminatorPropertyChanged
5859
&& ChangedUtils.isUnchanged(addPropChangedSchema) && ChangedUtils.isUnchanged(changedOneOfSchema)
59-
&& ChangedUtils.isUnchanged(changedExtensions)) {
60+
&& ChangedUtils.isUnchanged(changedItems) && ChangedUtils.isUnchanged(changedExtensions)) {
6061
return DiffResult.NO_CHANGES;
6162
}
6263
boolean backwardCompatibleForRequest = (changeEnum == null || changeEnum.getMissing().isEmpty()) &&
@@ -76,7 +77,7 @@ public DiffResult isChanged() {
7677
&& !changedType && !discriminatorPropertyChanged && ChangedUtils.isCompatible(changedOneOfSchema)
7778
&& ChangedUtils.isCompatible(addPropChangedSchema)
7879
&& changedProperties.values().stream().allMatch(Changed::isCompatible)
79-
&& ChangedUtils.isCompatible(changedExtensions)) {
80+
&& ChangedUtils.isCompatible(changedItems) && ChangedUtils.isCompatible(changedExtensions)) {
8081
return DiffResult.COMPATIBLE;
8182
}
8283

src/main/java/com/qdesrame/openapi/diff/output/MarkdownRender.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

18-
import java.util.Arrays;
1918
import java.util.List;
2019
import java.util.Map;
2120

@@ -234,6 +233,9 @@ protected String schema(int deepness, ChangedSchema schema) {
234233
sb.append(required(deepness, "New required properties", schema.getChangeRequired().getIncreased()));
235234
sb.append(required(deepness, "New optional properties", schema.getChangeRequired().getMissing()));
236235
}
236+
if (schema.getChangedItems() != null) {
237+
sb.append(items(deepness, schema.getChangedItems()));
238+
}
237239
sb.append(listDiff(deepness, "enum", schema.getChangeEnum()));
238240
sb.append(properties(deepness, "Added property", schema.getIncreasedProperties(), true, schema.getContext()));
239241
sb.append(properties(deepness, "Deleted property", schema.getMissingProperties(), false, schema.getContext()));
@@ -271,14 +273,27 @@ protected String schema(int deepness, Schema schema, DiffContext context) {
271273
return sb.toString();
272274
}
273275

274-
protected String items(int deepness, Schema schema, DiffContext context) {
276+
protected String items(int deepness, ChangedSchema schema) {
275277
StringBuilder sb = new StringBuilder("");
276-
sb.append(format("%sItems (%s)%s\n", indent(deepness), type(schema), Arrays.asList("object", "array").contains(type(schema)) ? " :\n" : ""));
277-
description(indent(deepness + 1), schema.getDescription());
278-
sb.append(schema(deepness, schema, context));
278+
String type = type(schema.getNewSchema());
279+
if (schema.isChangedType()) {
280+
type = type(schema.getOldSchema()) + " -> " + type(schema.getNewSchema());
281+
}
282+
sb.append(items(deepness, "Changed items", type, schema.getNewSchema().getDescription()));
283+
sb.append(schema(deepness, schema));
279284
return sb.toString();
280285
}
281286

287+
protected String items(int deepness, Schema schema, DiffContext context) {
288+
return items(deepness, "Items", type(schema), schema.getDescription()) +
289+
schema(deepness, schema, context);
290+
}
291+
292+
protected String items(int deepness, String title, String type, String description) {
293+
return format("%s%s (%s):" +
294+
"\n%s\n", indent(deepness), title, type, description(indent(deepness + 1), description));
295+
}
296+
282297
protected String properties(final int deepness, String title, Map<String, Schema> properties, boolean showContent, DiffContext context) {
283298
StringBuilder sb = new StringBuilder("");
284299
if (properties != null) {

0 commit comments

Comments
 (0)