Skip to content

Commit 6e156b6

Browse files
committed
Add required info in context
1 parent 258c7d4 commit 6e156b6

File tree

9 files changed

+32
-14
lines changed

9 files changed

+32
-14
lines changed

src/main/java/com/qdesrame/openapi/diff/compare/ContentDiff.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Optional<ChangedContent> diff(Content left, Content right, DiffContext co
3636
MediaType oldMediaType = left.get(mediaTypeKey);
3737
MediaType newMediaType = right.get(mediaTypeKey);
3838
ChangedMediaType changedMediaType = new ChangedMediaType(oldMediaType.getSchema(), newMediaType.getSchema(), context);
39-
openApiDiff.getSchemaDiff().diff(new HashSet<>(), oldMediaType.getSchema(), newMediaType.getSchema(), context).ifPresent(changedMediaType::setChangedSchema);
39+
openApiDiff.getSchemaDiff().diff(new HashSet<>(), oldMediaType.getSchema(), newMediaType.getSchema(), context.copyWithRequired(true)).ifPresent(changedMediaType::setChangedSchema);
4040
if (!isUnchanged(changedMediaType)) {
4141
changedMediaTypes.put(mediaTypeKey, changedMediaType);
4242
}

src/main/java/com/qdesrame/openapi/diff/compare/HeaderDiff.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected Optional<ChangedHeader> computeDiff(HashSet<String> refSet, Header lef
4444
changedHeader.setChangeDeprecated(!Boolean.TRUE.equals(left.getDeprecated()) && Boolean.TRUE.equals(right.getDeprecated()));
4545
changedHeader.setChangeStyle(!Objects.equals(left.getStyle(), right.getStyle()));
4646
changedHeader.setChangeExplode(getBooleanDiff(left.getExplode(), right.getExplode()));
47-
openApiDiff.getSchemaDiff().diff(new HashSet<>(), left.getSchema(), right.getSchema(), context).ifPresent(changedHeader::setChangedSchema);
47+
openApiDiff.getSchemaDiff().diff(new HashSet<>(), left.getSchema(), right.getSchema(), context.copyWithRequired(true)).ifPresent(changedHeader::setChangedSchema);
4848
openApiDiff.getContentDiff().diff(left.getContent(), right.getContent(), context).ifPresent(changedHeader::setChangedContent);
4949

5050
return isChanged(changedHeader);

src/main/java/com/qdesrame/openapi/diff/compare/ParameterDiff.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected Optional<ChangedParameter> computeDiff(HashSet<String> refSet, Paramet
4646
changedParameter.setChangeAllowEmptyValue(getBooleanDiff(left.getAllowEmptyValue(), right.getAllowEmptyValue()));
4747
changedParameter.setChangeStyle(!Objects.equals(left.getStyle(), right.getStyle()));
4848
changedParameter.setChangeExplode(getBooleanDiff(left.getExplode(), right.getExplode()));
49-
Optional<ChangedSchema> changedSchema = openApiDiff.getSchemaDiff().diff(refSet, left.getSchema(), right.getSchema(), context);
49+
Optional<ChangedSchema> changedSchema = openApiDiff.getSchemaDiff().diff(refSet, left.getSchema(), right.getSchema(), context.copyWithRequired(true));
5050
if (changedSchema.isPresent()) {
5151
changedParameter.setChangedSchema(changedSchema.get());
5252
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ 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);
25+
return openApiDiff.getSchemaDiff().diff(refSet, leftArraySchema.getItems(), rightArraySchema.getItems(), context.copyWithRequired(true));
2626
}
2727
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Optional<ChangedSchema> diff(HashSet<String> refSet, Components leftCompo
6666
leftSchema.set$ref(leftMapping.get(key));
6767
Schema rightSchema = new Schema();
6868
rightSchema.set$ref(rightMapping.get(key));
69-
Optional<ChangedSchema> changedSchema = openApiDiff.getSchemaDiff().diff(refSet, leftSchema, rightSchema, context);
69+
Optional<ChangedSchema> changedSchema = openApiDiff.getSchemaDiff().diff(refSet, leftSchema, rightSchema, context.copyWithRequired(true));
7070
changedSchema.ifPresent(schema -> changedMapping.put(key, schema));
7171
}
7272
changedSchema.setChangedOneOfSchema(changedOneOfSchema);

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import io.swagger.v3.oas.models.media.Schema;
1313
import lombok.Getter;
1414

15-
import java.util.HashSet;
16-
import java.util.Map;
17-
import java.util.Objects;
18-
import java.util.Optional;
15+
import java.util.*;
1916

2017
import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;
2118

@@ -58,7 +55,7 @@ public Optional<ChangedSchema> diff(HashSet<String> refSet, Components leftCompo
5855
Map<String, Schema> missingProp = propertyDiff.getMissing();
5956

6057
for (String key : propertyDiff.getSharedKey()) {
61-
Optional<ChangedSchema> resultSchema = openApiDiff.getSchemaDiff().diff(refSet, leftProperties.get(key), rightProperties.get(key), context);
58+
Optional<ChangedSchema> resultSchema = openApiDiff.getSchemaDiff().diff(refSet, leftProperties.get(key), rightProperties.get(key), required(context, key, right.getRequired()));
6259
resultSchema.ifPresent(changedSchema1 -> changedSchema.getChangedProperties().put(key, changedSchema1));
6360
}
6461

@@ -69,6 +66,10 @@ public Optional<ChangedSchema> diff(HashSet<String> refSet, Components leftCompo
6966
return isChanged(changedSchema);
7067
}
7168

69+
private DiffContext required(DiffContext context, String key, List<String> required) {
70+
return context.copyWithRequired(required != null && required.contains(key));
71+
}
72+
7273
private void compareAdditionalProperties(HashSet<String> refSet, Schema leftSchema, Schema rightSchema, DiffContext context) {
7374
Object left = leftSchema.getAdditionalProperties();
7475
Object right = rightSchema.getAdditionalProperties();
@@ -81,7 +82,7 @@ private void compareAdditionalProperties(HashSet<String> refSet, Schema leftSche
8182
apChangedSchema.setNewSchema(rightAdditionalSchema);
8283
if (left != null && right != null) {
8384
Optional<ChangedSchema> addPropChangedSchemaOP
84-
= openApiDiff.getSchemaDiff().diff(refSet, leftAdditionalSchema, rightAdditionalSchema, context);
85+
= openApiDiff.getSchemaDiff().diff(refSet, leftAdditionalSchema, rightAdditionalSchema, context.copyWithRequired(false));
8586
apChangedSchema = addPropChangedSchemaOP.orElse(apChangedSchema);
8687
}
8788
isChanged(apChangedSchema).ifPresent(changedSchema::setAddPropChangedSchema);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class DiffContext {
1717
private PathItem.HttpMethod method;
1818
private boolean response;
1919
private boolean request;
20+
private Boolean required;
2021

2122
public DiffContext() {
2223
parameters = new HashMap<>();
@@ -28,6 +29,10 @@ public DiffContext copyWithMethod(PathItem.HttpMethod method) {
2829
return copy().setMethod(method);
2930
}
3031

32+
public DiffContext copyWithRequired(boolean required) {
33+
return copy().setRequired(required);
34+
}
35+
3136
public DiffContext copyAsRequest() {
3237
return copy().setRequest();
3338
}
@@ -81,6 +86,7 @@ private DiffContext copy() {
8186
context.method = this.method;
8287
context.response = this.response;
8388
context.request = this.request;
89+
context.required = this.required;
8490
return context;
8591
}
8692

@@ -93,6 +99,15 @@ public DiffContext setParameters(Map<String, String> parameters) {
9399
return this;
94100
}
95101

102+
public Boolean isRequired() {
103+
return required;
104+
}
105+
106+
private DiffContext setRequired(boolean required) {
107+
this.required = required;
108+
return this;
109+
}
110+
96111
@Override
97112
public boolean equals(Object o) {
98113
if (this == o) return true;
@@ -107,6 +122,7 @@ public boolean equals(Object o) {
107122
.append(url, that.url)
108123
.append(parameters, that.parameters)
109124
.append(method, that.method)
125+
.append(required, that.required)
110126
.isEquals();
111127
}
112128

@@ -118,6 +134,7 @@ public int hashCode() {
118134
.append(method)
119135
.append(response)
120136
.append(request)
137+
.append(required)
121138
.toHashCode();
122139
}
123140
}

src/main/java/com/qdesrame/openapi/diff/model/schema/ChangedReadOnly.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public DiffResult isChanged() {
3030
}
3131
if (context.isRequest()) {
3232
if (Boolean.TRUE.equals(newValue)) {
33-
return DiffResult.COMPATIBLE;
34-
} else {
3533
return DiffResult.INCOMPATIBLE;
34+
} else {
35+
return context.isRequired() ? DiffResult.INCOMPATIBLE : DiffResult.COMPATIBLE;
3636
}
3737
}
3838
return DiffResult.UNKNOWN;

src/main/java/com/qdesrame/openapi/diff/model/schema/ChangedWriteOnly.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public DiffResult isChanged() {
3232
if (Boolean.TRUE.equals(newValue)) {
3333
return DiffResult.INCOMPATIBLE;
3434
} else {
35-
return DiffResult.COMPATIBLE;
35+
return context.isRequired() ? DiffResult.INCOMPATIBLE : DiffResult.COMPATIBLE;
3636
}
3737
}
3838
return DiffResult.UNKNOWN;

0 commit comments

Comments
 (0)