Skip to content

Commit 92bac72

Browse files
authored
fix: MediaType implementation of OpenApi 3.1 (#94)
OpenAPI 3.1 allows defining MediaTypes without a schema. But the current logic wasn't able to detect this, because vertx-json-schema is adding internal properties to each JsonSchema.
1 parent 4500569 commit 92bac72

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/main/java/io/vertx/openapi/contract/impl/MediaTypeImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public MediaTypeImpl(String identifier, JsonObject mediaTypeModel) {
3333
throw createUnsupportedFeature("Media Type without a schema");
3434
}
3535

36-
if (mediaTypeModel.isEmpty()) {
36+
boolean emptySchema = mediaTypeModel
37+
.fieldNames().stream()
38+
// Ignore all the internal json-schema annotations, they start and end with "__"
39+
.allMatch(name -> name.startsWith("__") && name.endsWith("__"));
40+
41+
if (emptySchema) {
3742
// OpenAPI 3.1 allows defining MediaTypes without a schema.
3843
schema = null;
3944
} else {

src/test/java/io/vertx/tests/contract/impl/MediaTypeImplTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.vertx.openapi.contract.MediaType;
1818
import io.vertx.openapi.contract.OpenAPIContractException;
1919
import io.vertx.openapi.contract.impl.MediaTypeImpl;
20+
import java.util.Map;
2021
import org.junit.jupiter.api.Test;
2122
import org.junit.jupiter.params.ParameterizedTest;
2223
import org.junit.jupiter.params.provider.Arguments;
@@ -33,12 +34,40 @@
3334

3435
class MediaTypeImplTest {
3536
private static final String DUMMY_IDENTIFIER = APPLICATION_JSON.toString();
37+
private static final String DUMMY_REF = "__dummy_unknown_ref__";
38+
private static final String ABS_URI = "__absolute_uri__";
39+
private static final String ABS_RECURSIVE_REF = "__absolute_recursive_ref__";
40+
private static final String ABS_REF = "__absolute_ref__";
41+
private static final String DUMMY_REF_VALUE = "dummy-ref-value";
3642

3743
private static Stream<Arguments> testGetters() {
44+
var partialSchemaJson = JsonObject.of("schema", stringSchema().toJson());
45+
46+
var partialUnknownAnnotation = JsonObject.of(DUMMY_REF, DUMMY_REF_VALUE);
47+
var partialAbsoluteUri = JsonObject.of(ABS_URI, DUMMY_REF_VALUE);
48+
var partialAbsoluteRecursiveRef = JsonObject.of(ABS_RECURSIVE_REF, DUMMY_REF_VALUE);
49+
var partialAbsoluteRef = JsonObject.of(ABS_REF, DUMMY_REF_VALUE);
50+
var partialMultipleAnnotations = partialAbsoluteUri.copy().mergeIn(partialUnknownAnnotation);
51+
3852
return Stream.of(
39-
Arguments.of("MediaType model defined", new JsonObject().put("schema", stringSchema().toJson()), List.of("type"
40-
, "$id")),
41-
Arguments.of("No MediaType model defined", EMPTY_JSON_OBJECT, List.of())
53+
Arguments.of("MediaType model defined, with no internal annotations", partialSchemaJson,
54+
List.of("type", "$id")),
55+
Arguments.of("MediaType model defined, with an unknown internal annotation", partialSchemaJson
56+
.copy().mergeIn(partialUnknownAnnotation), List.of("type", "$id")),
57+
Arguments.of("MediaType model defined, with multiple internal annotations", partialSchemaJson
58+
.copy().mergeIn(partialMultipleAnnotations), List.of("type", "$id")),
59+
Arguments.of("No MediaType model defined, with an unknown internal annotation",
60+
partialUnknownAnnotation, List.of()),
61+
Arguments.of("No MediaType model defined, with absolute_uri internal annotation",
62+
partialAbsoluteUri, List.of()),
63+
Arguments.of("No MediaType model defined, with absolute_recursive_ref internal annotation",
64+
partialAbsoluteRecursiveRef, List.of()),
65+
Arguments.of("No MediaType model defined, with absolute_ref internal annotation",
66+
partialAbsoluteRef, List.of()),
67+
Arguments.of("No MediaType model defined, with multiple internal annotations",
68+
partialMultipleAnnotations, List.of()),
69+
Arguments.of("No MediaType model defined, with no internal annotations",
70+
EMPTY_JSON_OBJECT, List.of())
4271
);
4372
}
4473

0 commit comments

Comments
 (0)