Skip to content

Commit 56c40b2

Browse files
committed
Remove unnecessary properties
1 parent 6a7996d commit 56c40b2

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Map;
1616
import java.util.Objects;
1717
import java.util.Optional;
18+
import java.util.stream.Collectors;
1819

1920
import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;
2021

@@ -51,8 +52,6 @@ public Optional<ChangedSchema> diff(HashSet<String> refSet, Components leftCompo
5152
Map<String, Schema> leftProperties = null == left ? null : left.getProperties();
5253
Map<String, Schema> rightProperties = null == right ? null : right.getProperties();
5354
MapKeyDiff<String, Schema> propertyDiff = MapKeyDiff.diff(leftProperties, rightProperties);
54-
Map<String, Schema> increasedProp = propertyDiff.getIncreased();
55-
Map<String, Schema> missingProp = propertyDiff.getMissing();
5655

5756
for (String key : propertyDiff.getSharedKey()) {
5857
Optional<ChangedSchema> resultSchema = openApiDiff.getSchemaDiff().diff(refSet, leftProperties.get(key), rightProperties.get(key), context);
@@ -61,11 +60,22 @@ public Optional<ChangedSchema> diff(HashSet<String> refSet, Components leftCompo
6160

6261
compareAdditionalProperties(refSet, left, right, context);
6362

64-
changedSchema.getIncreasedProperties().putAll(increasedProp);
65-
changedSchema.getMissingProperties().putAll(missingProp);
63+
changedSchema.getIncreasedProperties().putAll(filterProperties(propertyDiff.getIncreased(), context));
64+
changedSchema.getMissingProperties().putAll(filterProperties(propertyDiff.getMissing(), context));
6665
return isChanged(changedSchema);
6766
}
6867

68+
private Map<String, Schema> filterProperties(Map<String, Schema> properties, DiffContext context) {
69+
return properties.entrySet().stream()
70+
.filter(entry -> isPropertyApplicable(entry.getValue(), context))
71+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
72+
}
73+
74+
private boolean isPropertyApplicable(Schema schema, DiffContext context) {
75+
return !(context.isResponse() && Boolean.TRUE.equals(schema.getWriteOnly()))
76+
&& !(context.isRequest() && Boolean.TRUE.equals(schema.getReadOnly()));
77+
}
78+
6979
private void compareAdditionalProperties(HashSet<String> refSet, Schema leftSchema, Schema rightSchema, DiffContext context) {
7080
Object left = leftSchema.getAdditionalProperties();
7181
Object right = rightSchema.getAdditionalProperties();

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

-15
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,6 @@ private String schema(ChangedSchema schema) {
216216
return schema(1, schema);
217217
}
218218

219-
private static boolean isDisplayed(ChangedSchema schema) {
220-
return isDisplayed(schema.getNewSchema(), schema.getContext());
221-
}
222-
223-
private static boolean isDisplayed(Schema schema, DiffContext context) {
224-
return !(Boolean.TRUE.equals(schema.getWriteOnly()) && context.isResponse())
225-
|| !(Boolean.TRUE.equals(schema.getReadOnly()) && context.isRequest());
226-
}
227-
228219
private String oneOfSchema(int deepness, ChangedOneOfSchema schema, String discriminator) {
229220
StringBuilder sb = new StringBuilder("");
230221
sb.append(format("%sSwitch `%s`:\n", indent(deepness), discriminator));
@@ -250,9 +241,6 @@ private String required(int deepness, String title, List<String> required) {
250241

251242
private String schema(int deepness, ChangedSchema schema) {
252243
StringBuilder sb = new StringBuilder("");
253-
if (!isDisplayed(schema)) {
254-
return sb.toString();
255-
}
256244
if (schema.isDiscriminatorPropertyChanged()) {
257245
LOGGER.debug("Discriminator property changed");
258246
}
@@ -292,9 +280,6 @@ private String schema(int deepness, ComposedSchema schema, DiffContext context)
292280

293281
private String schema(int deepness, Schema schema, DiffContext context) {
294282
StringBuilder sb = new StringBuilder("");
295-
if (!isDisplayed(schema, context)) {
296-
return sb.toString();
297-
}
298283
sb.append(listItem(deepness, "Enum", schema.getEnum()));
299284
sb.append(properties(deepness, "Property", schema.getProperties(), true, context));
300285
if (schema instanceof ComposedSchema) {

0 commit comments

Comments
 (0)