From f95621890a7a7160489539336020346c2299d206 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Mon, 21 Jun 2021 07:46:18 -0500 Subject: [PATCH 1/2] addd flag for media type example set value, also for value set on example class. --- .../v3/core/jackson/mixin/ExampleMixin.java | 23 +++++++ .../v3/core/jackson/mixin/MediaTypeMixin.java | 23 +++++++ .../v3/core/util/ObjectMapperFactory.java | 6 +- .../JsonDeserializationTest.java | 18 +++++ .../specFiles/media-type-null-example.yaml | 68 +++++++++++++++++++ .../v3/oas/models/examples/Example.java | 13 +++- .../v3/oas/models/media/MediaType.java | 13 +++- 7 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/ExampleMixin.java create mode 100644 modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/MediaTypeMixin.java create mode 100644 modules/swagger-core/src/test/resources/specFiles/media-type-null-example.yaml diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/ExampleMixin.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/ExampleMixin.java new file mode 100644 index 0000000000..2887e4a85c --- /dev/null +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/ExampleMixin.java @@ -0,0 +1,23 @@ +package io.swagger.v3.core.jackson.mixin; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.Map; + +public abstract class ExampleMixin { + + @JsonAnyGetter + public abstract Map getExtensions(); + + @JsonAnySetter + public abstract void addExtension(String name, Object value); + + @JsonInclude(JsonInclude.Include.CUSTOM) + public abstract Object getValue(); + + @JsonIgnore + public abstract boolean getValueSetFlag(); +} diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/MediaTypeMixin.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/MediaTypeMixin.java new file mode 100644 index 0000000000..1776eb2116 --- /dev/null +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/MediaTypeMixin.java @@ -0,0 +1,23 @@ +package io.swagger.v3.core.jackson.mixin; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.Map; + +public abstract class MediaTypeMixin { + + @JsonAnyGetter + public abstract Map getExtensions(); + + @JsonAnySetter + public abstract void addExtension(String name, Object value); + + @JsonIgnore + public abstract boolean getExampleSetFlag(); + + @JsonInclude(JsonInclude.Include.CUSTOM) + public abstract Object getExample(); +} diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ObjectMapperFactory.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ObjectMapperFactory.java index 29283a2cca..ab92655626 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ObjectMapperFactory.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ObjectMapperFactory.java @@ -17,7 +17,9 @@ import io.swagger.v3.core.jackson.SchemaSerializer; import io.swagger.v3.core.jackson.mixin.ComponentsMixin; import io.swagger.v3.core.jackson.mixin.DateSchemaMixin; +import io.swagger.v3.core.jackson.mixin.ExampleMixin; import io.swagger.v3.core.jackson.mixin.ExtensionsMixin; +import io.swagger.v3.core.jackson.mixin.MediaTypeMixin; import io.swagger.v3.core.jackson.mixin.OpenAPIMixin; import io.swagger.v3.core.jackson.mixin.OperationMixin; import io.swagger.v3.core.jackson.mixin.SchemaMixin; @@ -107,14 +109,14 @@ public JsonSerializer modifySerializer( sourceMixins.put(Contact.class, ExtensionsMixin.class); sourceMixins.put(Encoding.class, ExtensionsMixin.class); sourceMixins.put(EncodingProperty.class, ExtensionsMixin.class); - sourceMixins.put(Example.class, ExtensionsMixin.class); + sourceMixins.put(Example.class, ExampleMixin.class); sourceMixins.put(ExternalDocumentation.class, ExtensionsMixin.class); sourceMixins.put(Header.class, ExtensionsMixin.class); sourceMixins.put(Info.class, ExtensionsMixin.class); sourceMixins.put(License.class, ExtensionsMixin.class); sourceMixins.put(Link.class, ExtensionsMixin.class); sourceMixins.put(LinkParameter.class, ExtensionsMixin.class); - sourceMixins.put(MediaType.class, ExtensionsMixin.class); + sourceMixins.put(MediaType.class, MediaTypeMixin.class); sourceMixins.put(OAuthFlow.class, ExtensionsMixin.class); sourceMixins.put(OAuthFlows.class, ExtensionsMixin.class); sourceMixins.put(OpenAPI.class, OpenAPIMixin.class); diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java index cba693de35..ea0725ee9f 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java @@ -17,8 +17,10 @@ import io.swagger.v3.oas.models.responses.ApiResponses; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; +import org.apache.commons.io.FileUtils; import org.testng.annotations.Test; +import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -409,4 +411,20 @@ public void testNullExampleDeserialization() throws Exception { Yaml.prettyPrint(oas); } + @Test + public void testExampleDeserializationOnMediaType() throws Exception { + String content = FileUtils.readFileToString(new File("src/test/resources/specFiles/media-type-null-example.yaml"), "UTF-8"); + OpenAPI openAPI = Yaml.mapper().readValue(content, OpenAPI.class); + + assertNull(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExample()); + assertTrue(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExampleSetFlag()); + + assertNull(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExample()); + assertFalse(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExampleSetFlag()); + + assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample()); + + assertTrue(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExampleSetFlag()); + } + } diff --git a/modules/swagger-core/src/test/resources/specFiles/media-type-null-example.yaml b/modules/swagger-core/src/test/resources/specFiles/media-type-null-example.yaml new file mode 100644 index 0000000000..917ec5611a --- /dev/null +++ b/modules/swagger-core/src/test/resources/specFiles/media-type-null-example.yaml @@ -0,0 +1,68 @@ +openapi: "3.0.1" +info: + version: 1.0.0 + title: Swagger Petstore + license: + name: MIT +servers: + - url: https://petstore3.swagger.io/api/v3 +paths: + /pet: + post: + tags: + - pet + summary: Add a new pet to the store + operationId: addPet + requestBody: + description: Pet object that needs to be added to the store + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + example: + id: 10 + name: kitty + tag: something + required: true + responses: + 200: + description: Expected response to a valid request + content: + application/json: + schema: + $ref: "#/components/schemas/Pet" + /pets/{petId}: + get: + summary: Info for a specific pet + operationId: showPetById + tags: + - pets + parameters: + - name: petId + in: path + required: true + description: The id of the pet to retrieve + schema: + type: string + responses: + 200: + description: Expected response to a valid request + content: + application/json: + schema: + $ref: "#/components/schemas/Pet" + example: null +components: + schemas: + Pet: + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string diff --git a/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/examples/Example.java b/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/examples/Example.java index 552a6bd5c8..d6c5765cb4 100644 --- a/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/examples/Example.java +++ b/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/examples/Example.java @@ -28,6 +28,8 @@ public class Example { private String $ref = null; private java.util.Map extensions = null; + private boolean valueSetFlag; + /** * returns the summary property from a Example instance. * @@ -78,10 +80,11 @@ public Object getValue() { public void setValue(Object value) { this.value = value; + valueSetFlag = true; } public Example value(Object value) { - this.value = value; + setValue(value); return this; } @@ -143,6 +146,14 @@ public Example extensions(java.util.Map extensions) { return this; } + public boolean getValueSetFlag() { + return valueSetFlag; + } + + public void setValueSetFlag(boolean valueSetFlag) { + this.valueSetFlag = valueSetFlag; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java b/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java index f3995bc23a..3f0689b1a7 100644 --- a/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java +++ b/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java @@ -35,6 +35,8 @@ public class MediaType { private Map encoding = null; private java.util.Map extensions = null; + private boolean exampleSetFlag; + /** * returns the schema property from a MediaType instance. * @@ -93,10 +95,11 @@ public Object getExample() { public void setExample(Object example) { this.example = example; + exampleSetFlag = true; } public MediaType example(Object example) { - this.example = example; + setExample(example); return this; } @@ -127,6 +130,14 @@ public MediaType addEncoding(String key, Encoding encodingItem) { return this; } + public boolean getExampleSetFlag() { + return exampleSetFlag; + } + + public void setExampleSetFlag(boolean exampleSetFlag) { + this.exampleSetFlag = exampleSetFlag; + } + @Override public boolean equals(java.lang.Object o) { if (this == o) { From f18169a046f9ad8a1656031e70138b5e3cfaa3bd Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Mon, 21 Jun 2021 21:19:23 -0500 Subject: [PATCH 2/2] refactored setExample method on mediatype class --- .../io/swagger/v3/oas/models/media/MediaType.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java b/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java index 3f0689b1a7..3d7b8aa1b1 100644 --- a/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java +++ b/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/MediaType.java @@ -94,8 +94,15 @@ public Object getExample() { } public void setExample(Object example) { - this.example = example; - exampleSetFlag = true; + if (this.schema == null) { + this.example = example; + this.exampleSetFlag = true; + return; + } + this.example = this.schema.cast(example); + if (!(example != null && this.example == null)) { + this.exampleSetFlag = true; + } } public MediaType example(Object example) {