Skip to content

Commit

Permalink
#65: Support for dotted parameter names for required changes
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics committed Sep 14, 2022
1 parent f8c48bf commit ae2db29
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
@ToString
@RequiredArgsConstructor
public class Schema {
public static final String LEVEL_DELIMITER_REPLACE_VALUE = "$";
public static final String LEVEL_DELIMITER = ".";
private final String type;
private final Set<String> enumValues;
private final Set<SchemaAttribute> schemaAttributes;
Expand Down Expand Up @@ -114,12 +116,13 @@ public Set<String> getRequiredAttributeNames() {
Set<String> result = new HashSet<>();
for (Map.Entry<String, Boolean> entry : attributeRequiredMap.entrySet()) {
String attributeName = entry.getKey();
String originalAttributeName = attributeName.replace(LEVEL_DELIMITER_REPLACE_VALUE, LEVEL_DELIMITER);
Boolean attributeIsRequired = entry.getValue();
if (attributeName.contains(".")) {
if (attributeName.contains(LEVEL_DELIMITER)) {
boolean hierarchicallyNotRequired = false;
List<String> pieces = Arrays.asList(attributeName.split("\\."));
List<String> pieces = Arrays.asList(attributeName.split("\\" + LEVEL_DELIMITER));
for (int i = 0; i < pieces.size(); i++) {
StringJoiner stringJoiner = new StringJoiner(".");
StringJoiner stringJoiner = new StringJoiner(LEVEL_DELIMITER);
pieces.subList(0, i + 1).forEach(stringJoiner::add);
String attrToSearchFor = stringJoiner.toString();
boolean isRequired = BooleanUtils.toBoolean(attributeRequiredMap.get(attrToSearchFor));
Expand All @@ -130,12 +133,12 @@ public Set<String> getRequiredAttributeNames() {
}
if (!hierarchicallyNotRequired) {
if (attributeIsRequired) {
result.add(attributeName);
result.add(originalAttributeName);
}
}
} else {
if (attributeIsRequired) {
result.add(attributeName);
result.add(originalAttributeName);
}
}
}
Expand Down Expand Up @@ -206,9 +209,9 @@ private Map<String, Schema> internalGetSchemasRecursively(Collection<SchemaAttri

private String generateLeveledName(String name, String levelName) {
if (!StringUtils.isBlank(levelName)) {
return format("%s.%s", levelName, name);
return format("%s%s%s", levelName, LEVEL_DELIMITER, name);
}
return name;
return name.replace(LEVEL_DELIMITER, LEVEL_DELIMITER_REPLACE_VALUE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class SchemaAttribute implements Comparable<SchemaAttribute> {
private final Schema schema;
private final boolean required;

public String getName() {
return name.replace(Schema.LEVEL_DELIMITER_REPLACE_VALUE, Schema.LEVEL_DELIMITER);
}

public String getType() {
return schema.getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,19 @@ public void testRequestParameterRequiredWorksForPostObjects() {
assertThat(result).hasSize(1);
assertThat(result).hasSameElementsAs(expected);
}

@Test
public void testRequestParameterRequiredWorksForDottedPostObjects() {
// given
String oldApiPath = "swaggers/v2/request/dottedparameterrequired/swagger-old.json";
String newApiPath = "swaggers/v2/request/dottedparameterrequired/swagger-new.json";

RequestParameterRequiredBreakingChange bc1 = new RequestParameterRequiredBreakingChange("/pet", HttpMethod.POST, "urn:scim:schemas:extension:workspace:1.0:name");
Collection<BreakingChange> expected = Collections.singletonList(bc1);
// when
Collection<BreakingChange> result = execute(oldApiPath, newApiPath);
// then
assertThat(result).hasSize(1);
assertThat(result).hasSameElementsAs(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
{
"swagger": "2.0",
"info": {
"description": "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
"version": "1.0.5",
"title": "Swagger Petstore",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"email": "apiteam@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"host": "petstore.swagger.io",
"basePath": "/v2",
"tags": [
{
"name": "pet",
"description": "Everything about your Pets",
"externalDocs": {
"description": "Find out more",
"url": "http://swagger.io"
}
},
{
"name": "store",
"description": "Access to Petstore orders"
}
],
"schemes": [
"https",
"http"
],
"paths": {
"/pet": {
"post": {
"tags": [
"pet"
],
"summary": "Add a new pet to the store",
"description": "",
"operationId": "addPet",
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"in": "body",
"name": "body",
"description": "Pet object that needs to be added to the store",
"required": true,
"schema": {
"$ref": "#/definitions/Pet"
}
}
],
"responses": {
"405": {
"description": "Invalid input"
}
}
}
}
},
"definitions": {
"ApiResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"type": {
"type": "string"
},
"message": {
"type": "string"
}
}
},
"Category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "Category"
}
},
"Pet": {
"type": "object",
"required": [
"photoUrls",
"urn:scim:schemas:extension:workspace:1.0:name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"$ref": "#/definitions/Category"
},
"urn:scim:schemas:extension:workspace:1.0:name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"wrapped": true
},
"items": {
"type": "string",
"xml": {
"name": "photoUrl"
}
}
},
"tags": {
"type": "array",
"xml": {
"wrapped": true
},
"items": {
"xml": {
"name": "tag"
},
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
},
"xml": {
"name": "Pet"
}
},
"Tag": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "Tag"
}
},
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"petId": {
"type": "integer",
"format": "int64"
},
"quantity": {
"type": "integer",
"format": "int32"
},
"shipDate": {
"type": "string",
"format": "date-time"
},
"status": {
"type": "string",
"description": "Order Status",
"enum": [
"placed",
"approved",
"delivered"
]
},
"complete": {
"type": "boolean"
}
},
"xml": {
"name": "Order"
}
}
},
"externalDocs": {
"description": "Find out more about Swagger",
"url": "http://swagger.io"
}
}
Loading

0 comments on commit ae2db29

Please sign in to comment.