Skip to content

Commit

Permalink
Prevent removal of deprecated request/response attribute from raising…
Browse files Browse the repository at this point in the history
… breaking change

see #97
  • Loading branch information
pkunze authored and galovics committed Sep 30, 2023
1 parent 98b2980 commit c918f7b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SchemaAttribute implements Comparable<SchemaAttribute> {
private final String name;
private final Schema schema;
private final boolean required;
private final boolean deprecated;

public String getName() {
return name.replace(Schema.LEVEL_DELIMITER_REPLACE_VALUE, Schema.LEVEL_DELIMITER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ private List<SchemaAttribute> getSchemaAttributes(io.swagger.v3.oas.models.media
io.swagger.v3.oas.models.media.Schema newInternalSchema = e.getValue();
Schema schema = internalTransform(newInternalSchema);
String attributeName = e.getKey();
Boolean deprecatedInSchema = newInternalSchema.getDeprecated();
boolean deprecated = deprecatedInSchema == null ? false : deprecatedInSchema;
boolean required = requiredAttributes.contains(attributeName);
result.add(new SchemaAttribute(attributeName, schema, required));
result.add(new SchemaAttribute(attributeName, schema, required, deprecated));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ public Collection<RequestTypeAttributeRemovedBreakingChange> checkRule(Specifica
Optional<Schema> newApiSchema = newRequestBody.get().getSchemaByMediaType(mediaType);
if (newApiSchema.isPresent()) {
Schema newSchema = newApiSchema.get();
Collection<String> oldAttributeNames = schema.getAttributeNames();
Set<SchemaAttribute> oldAttributes = schema.getSchemaAttributes();
Collection<String> newAttributeNames = newSchema.getAttributeNames();
for (String oldAttributeName : oldAttributeNames) {
for (SchemaAttribute oldAttribute : oldAttributes) {
if (oldAttribute.isDeprecated()) {
continue;
}

String oldAttributeName = oldAttribute.getName();

if (!newAttributeNames.contains(oldAttributeName)) {
breakingChanges.add(
new RequestTypeAttributeRemovedBreakingChange(path.getPath(), path.getMethod(), oldAttributeName));
new RequestTypeAttributeRemovedBreakingChange(path.getPath(),
path.getMethod(), oldAttributeName));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ public Collection<ResponseTypeAttributeRemovedBreakingChange> checkRule(Specific
Optional<Schema> newApiSchema = newResponse.getSchemaByMediaType(mediaType);
if (newApiSchema.isPresent()) {
Schema newSchema = newApiSchema.get();
Collection<String> oldAttributeNames = schema.getAttributeNames();
Set<SchemaAttribute> oldAttributes = schema.getSchemaAttributes();
Collection<String> newAttributeNames = newSchema.getAttributeNames();
for (String oldAttributeName : oldAttributeNames) {
for (SchemaAttribute oldAttribute : oldAttributes) {
if (oldAttribute.isDeprecated()) {
continue;
}

String oldAttributeName = oldAttribute.getName();

if (!newAttributeNames.contains(oldAttributeName)) {
breakingChanges.add(
new ResponseTypeAttributeRemovedBreakingChange(path.getPath(), path.getMethod(), apiResponse.getCode(), oldAttributeName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public void testTransformWorksForAllOfComposedSchema() {
composedSchema.setAllOf(ImmutableList.of(petRef, dogRef));

Schema expectedSchema = new SchemaBuilder("object").schemaAttributes(ImmutableList.of(
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false),
new SchemaAttribute("bark", new SchemaBuilder("string").build(), false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false)
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false, false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false, false),
new SchemaAttribute("bark", new SchemaBuilder("string").build(), false, false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false, false)
)).build();
// when
Schema result = withSchemaStore(schemaStore, () -> underTest.transform(composedSchema));
Expand Down Expand Up @@ -86,9 +86,9 @@ public void testTransformWorksForAllOfComposedSchemaWithSamePropertyNamesAndSame
composedSchema.setAllOf(ImmutableList.of(petRef, dogRef));

Schema expectedSchema = new SchemaBuilder("object").schemaAttributes(ImmutableList.of(
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false)
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false, false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false, false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false, false)
)).build();
// when
Schema result = withSchemaStore(schemaStore, () -> underTest.transform(composedSchema));
Expand Down Expand Up @@ -129,10 +129,10 @@ public void testTransformWorksForAllOfComposedSchemaWithSamePropertyNamesAndDiff
composedSchema.setAllOf(ImmutableList.of(petRef, dogRef, catRef));

Schema expectedSchema = new SchemaBuilder("object").schemaAttributes(ImmutableList.of(
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false),
new SchemaAttribute("meow", new SchemaBuilder("string").build(), false)
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false, false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false, false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false, false),
new SchemaAttribute("meow", new SchemaBuilder("string").build(), false, false)
)).build();
// when
Schema result = withSchemaStore(schemaStore, () -> underTest.transform(composedSchema));
Expand Down Expand Up @@ -173,10 +173,10 @@ public void testTransformWorksForAllOfComposedSchemaWithSamePropertyNamesAnd3Dif
composedSchema.setAllOf(ImmutableList.of(petRef, dogRef, catRef));

Schema expectedSchema = new SchemaBuilder("object").schemaAttributes(ImmutableList.of(
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false),
new SchemaAttribute("meow", new SchemaBuilder("string").build(), false)
new SchemaAttribute("id", new SchemaBuilder("integer").build(), false, false),
new SchemaAttribute("name", new SchemaBuilder("string").build(), false, false),
new SchemaAttribute("breed", new SchemaBuilder("string").build(), false, false),
new SchemaAttribute("meow", new SchemaBuilder("string").build(), false, false)
)).build();
// when
Schema result = withSchemaStore(schemaStore, () -> underTest.transform(composedSchema));
Expand Down

0 comments on commit c918f7b

Please sign in to comment.