-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#24 Change in type of response attribute is not detected
- Loading branch information
Showing
5 changed files
with
1,527 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...ain/java/io/redskap/swagger/brake/core/rule/request/RequestTypeChangedBreakingChange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.redskap.swagger.brake.core.rule.request; | ||
|
||
import static java.lang.String.format; | ||
|
||
import io.redskap.swagger.brake.core.BreakingChange; | ||
import io.redskap.swagger.brake.core.model.HttpMethod; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@EqualsAndHashCode | ||
@ToString | ||
public class RequestTypeChangedBreakingChange implements BreakingChange { | ||
private final String path; | ||
private final HttpMethod method; | ||
private final String attributeName; | ||
private final String fromType; | ||
private final String toType; | ||
|
||
@Override | ||
public String getMessage() { | ||
return format("%s type was changed in %s %s from %s to %s ", attributeName, method, path, fromType, toType); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...rake/src/main/java/io/redskap/swagger/brake/core/rule/request/RequestTypeChangedRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.redskap.swagger.brake.core.rule.request; | ||
|
||
import static java.util.function.Function.identity; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import io.redskap.swagger.brake.core.model.*; | ||
import io.redskap.swagger.brake.core.rule.BreakingChangeRule; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class RequestTypeChangedRule implements BreakingChangeRule<RequestTypeChangedBreakingChange> { | ||
@Override | ||
public Collection<RequestTypeChangedBreakingChange> checkRule(Specification oldApi, Specification newApi) { | ||
Set<RequestTypeChangedBreakingChange> breakingChanges = new HashSet<>(); | ||
for (Path path : oldApi.getPaths()) { | ||
Optional<Path> newApiPath = newApi.getPath(path); | ||
if (newApiPath.isPresent()) { | ||
Path newPath = newApiPath.get(); | ||
Optional<Request> requestBody = path.getRequestBody(); | ||
Optional<Request> newRequestBody = newPath.getRequestBody(); | ||
if (requestBody.isPresent() && newRequestBody.isPresent()) { | ||
for (Map.Entry<MediaType, Schema> entry : requestBody.get().getMediaTypes().entrySet()) { | ||
MediaType mediaType = entry.getKey(); | ||
Schema schema = entry.getValue(); | ||
Optional<Schema> newApiSchema = newRequestBody.get().getSchemaByMediaType(mediaType); | ||
if (newApiSchema.isPresent()) { | ||
Schema newSchema = newApiSchema.get(); | ||
Collection<SchemaAttribute> oldSchemaAttributes = schema.getSchemaAttributes(); | ||
if (CollectionUtils.isNotEmpty(oldSchemaAttributes)) { | ||
Map<String, SchemaAttribute> newSchemaAttributesMap = | ||
newSchema.getSchemaAttributes().stream().collect(Collectors.toMap(SchemaAttribute::getName, identity())); | ||
for (SchemaAttribute oldSchemaAttribute : oldSchemaAttributes) { | ||
String oldAttributeName = oldSchemaAttribute.getName(); | ||
String oldAttributeType = oldSchemaAttribute.getSchema().getType(); | ||
SchemaAttribute newSchemaAttribute = newSchemaAttributesMap.get(oldAttributeName); | ||
if (newSchemaAttribute != null) { | ||
String newAttributeType = newSchemaAttribute.getSchema().getType(); | ||
if (!oldAttributeType.equalsIgnoreCase(newAttributeType)) { | ||
breakingChanges.add(new RequestTypeChangedBreakingChange(path.getPath(), path.getMethod(), oldAttributeName, | ||
oldAttributeType, newAttributeType)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return breakingChanges; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...src/test/java/io/redskap/swagger/brake/integration/request/RequestTypeChangedIntTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.redskap.swagger.brake.integration.request; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import io.redskap.swagger.brake.core.BreakingChange; | ||
import io.redskap.swagger.brake.core.model.HttpMethod; | ||
import io.redskap.swagger.brake.core.rule.request.RequestTypeChangedBreakingChange; | ||
import io.redskap.swagger.brake.integration.AbstractSwaggerBrakeIntTest; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class RequestTypeChangedIntTest extends AbstractSwaggerBrakeIntTest { | ||
@Test | ||
public void testRequestTypeChangeIsBreakingChange() { | ||
// given | ||
String oldApiPath = "request/attributetypechanged/petstore.yaml"; | ||
String newApiPath = "request/attributetypechanged/petstore_v2.yaml"; | ||
RequestTypeChangedBreakingChange bc1 = new RequestTypeChangedBreakingChange("/pet", HttpMethod.PUT, "id", "integer", "string"); | ||
RequestTypeChangedBreakingChange bc2 = new RequestTypeChangedBreakingChange("/pet", HttpMethod.POST, "id", "integer", "string"); | ||
Collection<BreakingChange> expected = Arrays.asList(bc1, bc2); | ||
// when | ||
Collection<BreakingChange> result = execute(oldApiPath, newApiPath); | ||
// then | ||
assertThat(result).hasSize(2); | ||
assertThat(result).hasSameElementsAs(expected); | ||
} | ||
} |
Oops, something went wrong.