Skip to content

Commit

Permalink
#14 Fix for allowing more broad mediatype than before
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics committed Dec 19, 2018
1 parent c89e837 commit 76f6fe6
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.redskap.swagger.brake.core.model;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;

@Getter
@EqualsAndHashCode
@ToString
public class MediaType {
public static final MediaType ALL = new MediaType("*/*");

private final MimeType mediaType;

public MediaType(String mediaType) {
this.mediaType = MimeTypeUtils.parseMimeType(mediaType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.Set;

import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -13,9 +14,17 @@
@EqualsAndHashCode
@ToString
public class Request {
private final Map<String, Schema> mediaTypes;
private final Map<MediaType, Schema> mediaTypes;

public Optional<Schema> getSchemaByMediaType(String mediaType) {
public Optional<Schema> getSchemaByMediaType(MediaType mediaType) {
return Optional.ofNullable(mediaTypes.get(mediaType));
}

public boolean isMediaTypeAllowed(MediaType mediaType) {
Set<MediaType> availableMediaTypes = mediaTypes.keySet();
if (availableMediaTypes.contains(MediaType.ALL)) {
return true;
}
return availableMediaTypes.contains(mediaType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.Set;

import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -14,9 +15,17 @@
@ToString
public class Response {
private final String code;
private final Map<String, Schema> mediaTypes;
private final Map<MediaType, Schema> mediaTypes;

public Optional<Schema> getSchemaByMediaType(String mediaType) {
public Optional<Schema> getSchemaByMediaType(MediaType mediaType) {
return Optional.ofNullable(mediaTypes.get(mediaType));
}

public boolean isMediaTypeAllowed(MediaType mediaType) {
Set<MediaType> availableMediaTypes = mediaTypes.keySet();
if (availableMediaTypes.contains(MediaType.ALL)) {
return true;
}
return availableMediaTypes.contains(mediaType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import java.util.Map;
import java.util.Set;

import io.redskap.swagger.brake.core.model.MediaType;
import io.redskap.swagger.brake.core.model.Response;
import io.redskap.swagger.brake.core.model.Schema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -22,11 +22,11 @@ public class ApiResponseTransformer implements Transformer<Pair<String, ApiRespo

@Override
public Response transform(Pair<String, ApiResponse> from) {
Map<String, Schema> schemaRefs = emptyMap();
Map<MediaType, Schema> schemaRefs = emptyMap();
Content content = from.getValue().getContent();
if (content != null) {
Set<Map.Entry<String, MediaType>> entries = content.entrySet();
schemaRefs = entries.stream().collect(toMap(Map.Entry::getKey, e -> mediaTypeTransformer.transform(e.getValue())));
Set<Map.Entry<String, io.swagger.v3.oas.models.media.MediaType>> entries = content.entrySet();
schemaRefs = entries.stream().collect(toMap(e -> new MediaType(e.getKey()), e -> mediaTypeTransformer.transform(e.getValue())));
}
return new Response(from.getKey(), schemaRefs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.Map;
import java.util.Set;

import io.redskap.swagger.brake.core.model.MediaType;
import io.redskap.swagger.brake.core.model.Request;
import io.redskap.swagger.brake.core.model.Schema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.parameters.RequestBody;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -19,8 +19,8 @@ public class RequestBodyTransformer implements Transformer<RequestBody, Request>

@Override
public Request transform(RequestBody from) {
Set<Map.Entry<String, MediaType>> entries = from.getContent().entrySet();
Map<String, Schema> mediaTypes = entries.stream().collect(toMap(Map.Entry::getKey, e -> mediaTypeTransformer.transform(e.getValue())));
Set<Map.Entry<String, io.swagger.v3.oas.models.media.MediaType>> entries = from.getContent().entrySet();
Map<MediaType, Schema> mediaTypes = entries.stream().collect(toMap(e -> new MediaType(e.getKey()), e -> mediaTypeTransformer.transform(e.getValue())));
return new Request(mediaTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.core.model.HttpMethod;
import io.redskap.swagger.brake.core.model.MediaType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,7 +17,7 @@
public class RequestMediaTypeDeletedBreakingChange implements BreakingChange {
private final String path;
private final HttpMethod method;
private final String mediaType;
private final MediaType mediaType;

@Override
public String getMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.util.*;

import io.redskap.swagger.brake.core.model.Path;
import io.redskap.swagger.brake.core.model.Request;
import io.redskap.swagger.brake.core.model.Schema;
import io.redskap.swagger.brake.core.model.Specification;
import io.redskap.swagger.brake.core.model.*;
import io.redskap.swagger.brake.core.rule.BreakingChangeRule;
import org.springframework.stereotype.Component;

Expand All @@ -21,15 +18,21 @@ public Collection<RequestMediaTypeDeletedBreakingChange> checkRule(Specification
Optional<Request> requestBody = path.getRequestBody();
Optional<Request> newRequestBody = newPath.getRequestBody();
if (requestBody.isPresent() && newRequestBody.isPresent()) {
for (Map.Entry<String, Schema> entry : requestBody.get().getMediaTypes().entrySet()) {
String mediaType = entry.getKey();
if (!newRequestBody.get().getMediaTypes().containsKey(mediaType)) {
breakingChanges.add(new RequestMediaTypeDeletedBreakingChange(path.getPath(), path.getMethod(), mediaType));
}
}
Request request = requestBody.get();
Request newRequest = newRequestBody.get();
checkMediaTypeBreaking(breakingChanges, path, request, newRequest);
}
}
}
return breakingChanges;
}

private void checkMediaTypeBreaking(Set<RequestMediaTypeDeletedBreakingChange> breakingChanges, Path path, Request request, Request newRequest) {
for (Map.Entry<MediaType, Schema> entry : request.getMediaTypes().entrySet()) {
MediaType mediaType = entry.getKey();
if (!newRequest.isMediaTypeAllowed(mediaType)) {
breakingChanges.add(new RequestMediaTypeDeletedBreakingChange(path.getPath(), path.getMethod(), mediaType));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.util.*;

import io.redskap.swagger.brake.core.model.Path;
import io.redskap.swagger.brake.core.model.Request;
import io.redskap.swagger.brake.core.model.Schema;
import io.redskap.swagger.brake.core.model.Specification;
import io.redskap.swagger.brake.core.model.*;
import io.redskap.swagger.brake.core.rule.BreakingChangeRule;
import org.springframework.stereotype.Component;

Expand All @@ -21,8 +18,8 @@ public Collection<RequestTypeAttributeRemovedBreakingChange> checkRule(Specifica
Optional<Request> requestBody = path.getRequestBody();
Optional<Request> newRequestBody = newPath.getRequestBody();
if (requestBody.isPresent() && newRequestBody.isPresent()) {
for (Map.Entry<String, Schema> entry : requestBody.get().getMediaTypes().entrySet()) {
String mediaType = entry.getKey();
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.util.*;

import io.redskap.swagger.brake.core.model.Path;
import io.redskap.swagger.brake.core.model.Request;
import io.redskap.swagger.brake.core.model.Schema;
import io.redskap.swagger.brake.core.model.Specification;
import io.redskap.swagger.brake.core.model.*;
import io.redskap.swagger.brake.core.rule.BreakingChangeRule;
import org.springframework.stereotype.Component;

Expand All @@ -21,8 +18,8 @@ public Collection<RequestTypeEnumValueDeletedBreakingChange> checkRule(Specifica
Optional<Request> requestBody = path.getRequestBody();
Optional<Request> newRequestBody = newPath.getRequestBody();
if (requestBody.isPresent() && newRequestBody.isPresent()) {
for (Map.Entry<String, Schema> entry : requestBody.get().getMediaTypes().entrySet()) {
String mediaType = entry.getKey();
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.core.model.HttpMethod;
import io.redskap.swagger.brake.core.model.MediaType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,7 +17,7 @@
public class ResponseMediaTypeDeletedBreakingChange implements BreakingChange {
private final String path;
private final HttpMethod method;
private final String mediaType;
private final MediaType mediaType;

@Override
public String getMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.util.*;

import io.redskap.swagger.brake.core.model.Path;
import io.redskap.swagger.brake.core.model.Response;
import io.redskap.swagger.brake.core.model.Schema;
import io.redskap.swagger.brake.core.model.Specification;
import io.redskap.swagger.brake.core.model.*;
import io.redskap.swagger.brake.core.rule.BreakingChangeRule;
import org.springframework.stereotype.Component;

Expand All @@ -22,16 +19,20 @@ public Collection<ResponseMediaTypeDeletedBreakingChange> checkRule(Specificatio
Optional<Response> newApiResponse = newPath.getResponseByCode(apiResponse.getCode());
if (newApiResponse.isPresent()) {
Response newResponse = newApiResponse.get();
for (Map.Entry<String, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
String mediaType = entry.getKey();
if (!newResponse.getMediaTypes().containsKey(mediaType)) {
breakingChanges.add(new ResponseMediaTypeDeletedBreakingChange(path.getPath(), path.getMethod(), mediaType));
}
}
checkMediaTypeBreaking(breakingChanges, path, apiResponse, newResponse);
}
}
}
}
return breakingChanges;
}

private void checkMediaTypeBreaking(Set<ResponseMediaTypeDeletedBreakingChange> breakingChanges, Path path, Response apiResponse, Response newResponse) {
for (Map.Entry<MediaType, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
MediaType mediaType = entry.getKey();
if (!newResponse.isMediaTypeAllowed(mediaType)) {
breakingChanges.add(new ResponseMediaTypeDeletedBreakingChange(path.getPath(), path.getMethod(), mediaType));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.util.*;

import io.redskap.swagger.brake.core.model.Path;
import io.redskap.swagger.brake.core.model.Response;
import io.redskap.swagger.brake.core.model.Schema;
import io.redskap.swagger.brake.core.model.Specification;
import io.redskap.swagger.brake.core.model.*;
import io.redskap.swagger.brake.core.rule.BreakingChangeRule;
import org.springframework.stereotype.Component;

Expand All @@ -22,8 +19,8 @@ public Collection<ResponseTypeAttributeRemovedBreakingChange> checkRule(Specific
Optional<Response> newApiResponse = newPath.getResponseByCode(apiResponse.getCode());
if (newApiResponse.isPresent()) {
Response newResponse = newApiResponse.get();
for (Map.Entry<String, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
String mediaType = entry.getKey();
for (Map.Entry<MediaType, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
MediaType mediaType = entry.getKey();
Schema schema = entry.getValue();
Optional<Schema> newApiSchema = newResponse.getSchemaByMediaType(mediaType);
if (newApiSchema.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.util.*;

import io.redskap.swagger.brake.core.model.Path;
import io.redskap.swagger.brake.core.model.Response;
import io.redskap.swagger.brake.core.model.Schema;
import io.redskap.swagger.brake.core.model.Specification;
import io.redskap.swagger.brake.core.model.*;
import io.redskap.swagger.brake.core.rule.BreakingChangeRule;
import org.springframework.stereotype.Component;

Expand All @@ -22,8 +19,8 @@ public Collection<ResponseTypeChangedBreakingChange> checkRule(Specification old
Optional<Response> newApiResponse = newPath.getResponseByCode(apiResponse.getCode());
if (newApiResponse.isPresent()) {
Response newResponse = newApiResponse.get();
for (Map.Entry<String, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
String mediaType = entry.getKey();
for (Map.Entry<MediaType, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
MediaType mediaType = entry.getKey();
Schema schema = entry.getValue();
Optional<Schema> newApiSchema = newResponse.getSchemaByMediaType(mediaType);
if (newApiSchema.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.util.*;

import io.redskap.swagger.brake.core.model.Path;
import io.redskap.swagger.brake.core.model.Response;
import io.redskap.swagger.brake.core.model.Schema;
import io.redskap.swagger.brake.core.model.Specification;
import io.redskap.swagger.brake.core.model.*;
import io.redskap.swagger.brake.core.rule.BreakingChangeRule;
import org.springframework.stereotype.Component;

Expand All @@ -22,8 +19,8 @@ public Collection<ResponseTypeEnumValueDeletedBreakingChange> checkRule(Specific
Optional<Response> newApiResponse = newPath.getResponseByCode(apiResponse.getCode());
if (newApiResponse.isPresent()) {
Response newResponse = newApiResponse.get();
for (Map.Entry<String, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
String mediaType = entry.getKey();
for (Map.Entry<MediaType, Schema> entry : apiResponse.getMediaTypes().entrySet()) {
MediaType mediaType = entry.getKey();
Schema schema = entry.getValue();
Optional<Schema> newApiSchema = newResponse.getSchemaByMediaType(mediaType);
if (newApiSchema.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.core.model.HttpMethod;
import io.redskap.swagger.brake.core.model.MediaType;
import io.redskap.swagger.brake.core.rule.request.RequestMediaTypeDeletedBreakingChange;
import io.redskap.swagger.brake.integration.AbstractSwaggerBrakeIntTest;
import org.junit.Test;
Expand All @@ -20,7 +21,7 @@ public void testRequestMediaTypeDeletedWorksCorrectly() {
// given
String oldApiPath = "request/mediatypedeleted/petstore.yaml";
String newApiPath = "request/mediatypedeleted/petstore_v2.yaml";
RequestMediaTypeDeletedBreakingChange bc = new RequestMediaTypeDeletedBreakingChange("/pet", HttpMethod.POST, "application/xml");
RequestMediaTypeDeletedBreakingChange bc = new RequestMediaTypeDeletedBreakingChange("/pet", HttpMethod.POST, new MediaType("application/xml"));
Collection<BreakingChange> expected = Collections.singleton(bc);
// when
Collection<BreakingChange> result = execute(oldApiPath, newApiPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.core.model.HttpMethod;
import io.redskap.swagger.brake.core.model.MediaType;
import io.redskap.swagger.brake.core.rule.response.ResponseMediaTypeDeletedBreakingChange;
import io.redskap.swagger.brake.integration.AbstractSwaggerBrakeIntTest;
import org.junit.Test;
Expand All @@ -20,7 +21,7 @@ public void testResponseMediaTypeDeletedWorksCorrectly() {
// given
String oldApiPath = "response/mediatypedeleted/petstore.yaml";
String newApiPath = "response/mediatypedeleted/petstore_v2.yaml";
ResponseMediaTypeDeletedBreakingChange bc = new ResponseMediaTypeDeletedBreakingChange("/pet/findByStatus", HttpMethod.GET, "application/json");
ResponseMediaTypeDeletedBreakingChange bc = new ResponseMediaTypeDeletedBreakingChange("/pet/findByStatus", HttpMethod.GET, new MediaType("application/json"));
Collection<BreakingChange> expected = Collections.singleton(bc);
// when
Collection<BreakingChange> result = execute(oldApiPath, newApiPath);
Expand Down
Loading

0 comments on commit 76f6fe6

Please sign in to comment.