Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.vertx.json.schema.JsonSchema;
import io.vertx.openapi.contract.MediaType;
import io.vertx.openapi.contract.RequestBody;
import io.vertx.openapi.mediatype.MediaTypeInfo;
import io.vertx.openapi.mediatype.MediaTypeRegistration;
import io.vertx.openapi.mediatype.MediaTypeRegistry;
import java.util.Map;
Expand Down Expand Up @@ -95,7 +96,8 @@ public MediaType determineContentType(String contentType) {
}

for (Map.Entry<String, MediaType> declaredType : content.entrySet()) {
if (condensedIdentifier.startsWith(removeWhiteSpaces(declaredType.getKey()))) {
if (MediaTypeInfo.of(removeWhiteSpaces(declaredType.getKey()))
.doesInclude(MediaTypeInfo.of(condensedIdentifier))) {
return declaredType.getValue();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import io.vertx.openapi.contract.OpenAPIContractException;
import io.vertx.openapi.contract.RequestBody;
import io.vertx.openapi.contract.impl.RequestBodyImpl;
import io.vertx.openapi.mediatype.ContentAnalyserFactory;
import io.vertx.openapi.mediatype.MediaTypePredicate;
import io.vertx.openapi.mediatype.MediaTypeRegistry;
import io.vertx.openapi.mediatype.impl.DefaultMediaTypeRegistration;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -104,13 +107,16 @@ void testExceptions(String testId, ContractErrorType type, String msg) {
}

private RequestBodyImpl buildWithContent(String... contentTypes) {
return buildWithContentAndRegistry(MediaTypeRegistry.createDefault(), contentTypes);
}

private RequestBodyImpl buildWithContentAndRegistry(MediaTypeRegistry registry, String... contentTypes) {
JsonObject dummySchema = new JsonObject().put("schema", new JsonObject().put("type", "string"));
JsonObject content = new JsonObject();
for (String type : contentTypes) {
content.put(type, dummySchema);
}
return new RequestBodyImpl(new JsonObject().put("content", content), DUMMY_OPERATION_ID,
MediaTypeRegistry.createDefault());
return new RequestBodyImpl(new JsonObject().put("content", content), DUMMY_OPERATION_ID, registry);
}

@Test
Expand All @@ -133,5 +139,18 @@ void testDetermineContentType() {
.isEqualTo(APPLICATION_JSON_UTF8);

assertThat(bodyBoth.determineContentType("application/text")).isNull();

// With wildcard content type
}

@Test
void testDetermineContentTypeWithMediaTypeRange() {
String imageWildcard = "image/*";
MediaTypeRegistry registry = MediaTypeRegistry.createDefault()
.register(new DefaultMediaTypeRegistration(
MediaTypePredicate.ofRegexp("image/.*"), ContentAnalyserFactory.noop()));
RequestBody requestBody = buildWithContentAndRegistry(registry, imageWildcard);

assertThat(requestBody.determineContentType("image/png").getIdentifier()).isEqualTo(imageWildcard);
}
}