diff --git a/smithy-openapi/src/main/java/software/amazon/smithy/openapi/OpenApiVersion.java b/smithy-openapi/src/main/java/software/amazon/smithy/openapi/OpenApiVersion.java index 27d93908434..46a1a4fde23 100644 --- a/smithy-openapi/src/main/java/software/amazon/smithy/openapi/OpenApiVersion.java +++ b/smithy-openapi/src/main/java/software/amazon/smithy/openapi/OpenApiVersion.java @@ -15,25 +15,32 @@ package software.amazon.smithy.openapi; +import java.util.Set; import software.amazon.smithy.jsonschema.JsonSchemaVersion; +import software.amazon.smithy.utils.SetUtils; import software.amazon.smithy.utils.SmithyInternalApi; /** * OpenAPI versions supported by the converter. */ public enum OpenApiVersion { - - VERSION_3_0_2("3.0.2", false, JsonSchemaVersion.DRAFT07), - VERSION_3_1_0("3.1.0", true, JsonSchemaVersion.DRAFT2020_12); + VERSION_3_0_2("3.0.2", false, JsonSchemaVersion.DRAFT07, + SetUtils.of("propertyNames", "contentMediaType")), + VERSION_3_1_0("3.1.0", true, JsonSchemaVersion.DRAFT2020_12, + SetUtils.of("contentMediaType")); private final String version; private final boolean supportsContentEncodingKeyword; private final JsonSchemaVersion jsonSchemaVersion; + // See https://swagger.io/docs/specification/data-models/keywords/ for 3.0.2. + private final Set unsupportedKeywords; - OpenApiVersion(String version, boolean supportsContentEncodingKeyword, JsonSchemaVersion jsonSchemaVersion) { + OpenApiVersion(String version, boolean supportsContentEncodingKeyword, JsonSchemaVersion jsonSchemaVersion, + Set unsupportedKeywords) { this.version = version; this.supportsContentEncodingKeyword = supportsContentEncodingKeyword; this.jsonSchemaVersion = jsonSchemaVersion; + this.unsupportedKeywords = unsupportedKeywords; } @Override @@ -46,6 +53,11 @@ public boolean supportsContentEncodingKeyword() { return supportsContentEncodingKeyword; } + @SmithyInternalApi + public Set getUnsupportedKeywords() { + return unsupportedKeywords; + } + JsonSchemaVersion getJsonSchemaVersion() { return jsonSchemaVersion; } diff --git a/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapper.java b/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapper.java index 4a407eef0a9..8fecc349eba 100644 --- a/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapper.java +++ b/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapper.java @@ -22,7 +22,6 @@ import java.util.Locale; import java.util.Map; import java.util.Optional; -import java.util.Set; import software.amazon.smithy.jsonschema.JsonSchemaConfig; import software.amazon.smithy.jsonschema.JsonSchemaMapper; import software.amazon.smithy.jsonschema.Schema; @@ -35,7 +34,6 @@ import software.amazon.smithy.openapi.OpenApiConfig; import software.amazon.smithy.openapi.model.ExternalDocumentation; import software.amazon.smithy.utils.MapUtils; -import software.amazon.smithy.utils.SetUtils; /** * Applies OpenAPI extensions to a {@link Schema} using configuration settings @@ -46,11 +44,6 @@ */ public final class OpenApiJsonSchemaMapper implements JsonSchemaMapper { - /** See https://swagger.io/docs/specification/data-models/keywords/. */ - private static final Set UNSUPPORTED_KEYWORD_DIRECTIVES = SetUtils.of( - "propertyNames", - "contentMediaType"); - @Override public Schema.Builder updateSchema(Shape shape, Schema.Builder builder, JsonSchemaConfig config) { getResolvedExternalDocs(shape, config) @@ -93,7 +86,10 @@ public Schema.Builder updateSchema(Shape shape, Schema.Builder builder, JsonSche } // Remove unsupported JSON Schema keywords. - UNSUPPORTED_KEYWORD_DIRECTIVES.forEach(builder::disableProperty); + if (config instanceof OpenApiConfig) { + OpenApiConfig openApiConfig = (OpenApiConfig) config; + openApiConfig.getVersion().getUnsupportedKeywords().forEach(builder::disableProperty); + } return builder; } diff --git a/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiConverterTest.java b/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiConverterTest.java index 7c519fbc5ba..2c3fcdc802c 100644 --- a/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiConverterTest.java +++ b/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiConverterTest.java @@ -603,6 +603,7 @@ public void convertsToOpenAPI3_0_2() { Node.assertEquals(result, expectedNode); } + @Test public void convertsToOpenAPI3_1_0() { Model model = Model.assembler() diff --git a/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapperTest.java b/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapperTest.java index b1603c4b606..ceeee93edfa 100644 --- a/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapperTest.java +++ b/smithy-openapi/src/test/java/software/amazon/smithy/openapi/fromsmithy/OpenApiJsonSchemaMapperTest.java @@ -42,7 +42,6 @@ import software.amazon.smithy.model.shapes.ShortShape; import software.amazon.smithy.model.shapes.StringShape; import software.amazon.smithy.model.shapes.StructureShape; -import software.amazon.smithy.model.traits.BoxTrait; import software.amazon.smithy.model.traits.DeprecatedTrait; import software.amazon.smithy.model.traits.ExternalDocumentationTrait; import software.amazon.smithy.model.traits.SensitiveTrait; @@ -67,23 +66,6 @@ public void convertsModels() { assertTrue(document.toNode().expectObjectNode().getMember("components").isPresent()); } - @Test - public void stripsUnsupportedKeywords() { - StringShape string = StringShape.builder().id("smithy.api#String").build(); - MemberShape key = MemberShape.builder().id("smithy.example#Map$key").target("smithy.api#String").build(); - MemberShape value = MemberShape.builder().id("smithy.example#Map$value").target("smithy.api#String").build(); - MapShape shape = MapShape.builder().id("smithy.example#Map").key(key).value(value).build(); - Model model = Model.builder().addShapes(string, shape, key, value).build(); - SchemaDocument document = JsonSchemaConverter.builder() - .addMapper(new OpenApiJsonSchemaMapper()) - .model(model) - .build() - .convertShape(shape); - Schema schema = document.getRootSchema(); - - assertFalse(schema.getPropertyNames().isPresent()); - } - @Test public void supportsExternalDocs() { String key = "Homepage"; diff --git a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/nullability-and-format.smithy b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/nullability-and-format.smithy index b2f84b51a66..32bc3581a86 100644 --- a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/nullability-and-format.smithy +++ b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/nullability-and-format.smithy @@ -26,7 +26,8 @@ blob FilePayload @output structure FooBarOutput { - bar: BoxedInteger + bar: BoxedInteger, + baz: MyMap } @error("client") @@ -36,3 +37,18 @@ structure FooBarError { @box integer BoxedInteger + +map MyMap { + key: MyEnum, + value: String +} + +@enum([ + { + value: "FOO" + }, + { + value: "BAR" + } +]) +string MyEnum diff --git a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-0-2.openapi.json b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-0-2.openapi.json index 1293e4ffcbd..9425372b34a 100644 --- a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-0-2.openapi.json +++ b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-0-2.openapi.json @@ -69,8 +69,17 @@ "properties": { "bar": { "type": "number" + }, + "baz": { + "$ref": "#/components/schemas/MyMap" } } + }, + "MyMap": { + "type": "object", + "additionalProperties": { + "type": "string" + } } } } diff --git a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-1-0.openapi.json b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-1-0.openapi.json index 0c6c3b6b3b3..77c68195932 100644 --- a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-1-0.openapi.json +++ b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/openapi-3-1-0.openapi.json @@ -69,8 +69,27 @@ "properties": { "bar": { "type": "number" + }, + "baz": { + "$ref": "#/components/schemas/MyMap" } } + }, + "MyEnum": { + "type": "string", + "enum": [ + "FOO", + "BAR" + ] + }, + "MyMap": { + "type": "object", + "propertyNames": { + "$ref": "#/components/schemas/MyEnum" + }, + "additionalProperties": { + "type": "string" + } } } }