Skip to content

Commit

Permalink
deps update and adjust example (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Nov 15, 2022
1 parent 98a3e70 commit 6905f74
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.swagger.v3.core.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
import io.swagger.v3.oas.models.examples.Example;

import java.io.IOException;

public class ExampleSerializer extends JsonSerializer<Example> implements ResolvableSerializer {

private JsonSerializer<Object> defaultSerializer;

public ExampleSerializer(JsonSerializer<Object> serializer) {
defaultSerializer = serializer;
}

@Override
public void resolve(SerializerProvider serializerProvider) throws JsonMappingException {
if (defaultSerializer instanceof ResolvableSerializer) {
((ResolvableSerializer) defaultSerializer).resolve(serializerProvider);
}
}

@Override
public void serialize(
Example example, JsonGenerator jgen, SerializerProvider provider)
throws IOException {

if (example.getValueSetFlag() && example.getValue() == null) {
jgen.writeStartObject();
defaultSerializer.unwrappingSerializer(null).serialize(example, jgen, provider);
jgen.writeNullField("value");
jgen.writeEndObject();
} else {
defaultSerializer.serialize(example, jgen, provider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import java.util.Map;

public abstract class ExampleMixin {

@JsonAnyGetter
public abstract Map<String, Object> getExtensions();

@JsonAnySetter
public abstract void addExtension(String name, Object value);

@JsonInclude(JsonInclude.Include.CUSTOM)
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getValue();

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.swagger.v3.core.jackson.ExampleSerializer;
import io.swagger.v3.core.jackson.Schema31Serializer;
import io.swagger.v3.core.jackson.MediaTypeSerializer;
import io.swagger.v3.core.jackson.SchemaSerializer;
Expand Down Expand Up @@ -116,6 +117,8 @@ public JsonSerializer<?> modifySerializer(
return new SchemaSerializer((JsonSerializer<Object>) serializer);
} else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
} else if (Example.class.isAssignableFrom(desc.getBeanClass())) {
return new ExampleSerializer((JsonSerializer<Object>) serializer);
}
return serializer;
}
Expand All @@ -135,6 +138,8 @@ public JsonSerializer<?> modifySerializer(
return new Schema31Serializer((JsonSerializer<Object>) serializer);
} else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
} else if (Example.class.isAssignableFrom(desc.getBeanClass())) {
return new ExampleSerializer((JsonSerializer<Object>) serializer);
}
return serializer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Encoding;
Expand Down Expand Up @@ -411,6 +412,120 @@ public void testNullExampleDeserialization() throws Exception {
Yaml.prettyPrint(oas);
}

@Test
public void testNullExampleAndValues() throws Exception {
String yamlNull = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
"components:\n" +
" schemas:\n" +
" UserStatus:\n" +
" type: object\n" +
" example: null\n";

String yamlMissing = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
"components:\n" +
" schemas:\n" +
" UserStatus:\n" +
" type: object\n";

String yamlNotNull = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
"components:\n" +
" schemas:\n" +
" UserStatus:\n" +
" type: object\n" +
" example:\n" +
" value: bar\n";

String yamlValueNull = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
"components:\n" +
" examples:\n" +
" UserStatus:\n" +
" summary: string\n" +
" value: null\n";

String yamlValueMissing = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
"components:\n" +
" examples:\n" +
" UserStatus:\n" +
" summary: string\n";

String yamlValueNotNull = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
"components:\n" +
" examples:\n" +
" UserStatus:\n" +
" summary: string\n" +
" value: bar\n";

OpenAPI oas = Yaml.mapper().readValue(yamlNull, OpenAPI.class);
Yaml.prettyPrint(oas);

assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
assertTrue(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
assertEquals(Yaml.pretty(oas), yamlNull);

oas = Yaml.mapper().readValue(yamlMissing, OpenAPI.class);
Yaml.prettyPrint(oas);
assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
assertFalse(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
assertEquals(Yaml.pretty(oas), yamlMissing);

oas = Yaml.mapper().readValue(yamlNotNull, OpenAPI.class);
Yaml.prettyPrint(oas);
assertNotNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
assertTrue(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
assertEquals(Yaml.pretty(oas), yamlNotNull);

oas = Yaml.mapper().readValue(yamlValueNull, OpenAPI.class);
Yaml.prettyPrint(oas);
Example ex = oas.getComponents().getExamples().get("UserStatus");
assertNull(ex.getValue());
assertTrue(ex.getValueSetFlag());
assertEquals(Yaml.pretty(oas), yamlValueNull);

oas = Yaml.mapper().readValue(yamlValueMissing, OpenAPI.class);
Yaml.prettyPrint(oas);
ex = oas.getComponents().getExamples().get("UserStatus");
assertNull(ex.getValue());
assertFalse(ex.getValueSetFlag());
assertEquals(Yaml.pretty(oas), yamlValueMissing);

oas = Yaml.mapper().readValue(yamlValueNotNull, OpenAPI.class);
Yaml.prettyPrint(oas);
ex = oas.getComponents().getExamples().get("UserStatus");
assertNotNull(ex.getValue());
assertTrue(ex.getValueSetFlag());
assertEquals(Yaml.pretty(oas), yamlValueNotNull);
}

@Test
public void testExampleDeserializationOnMediaType() throws Exception {
String content = FileUtils.readFileToString(new File("src/test/resources/specFiles/media-type-null-example.yaml"), "UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class OpenAPI3_1SerializationTest {

Expand Down Expand Up @@ -1419,6 +1420,83 @@ public void testBooleanSchemaSerialization() {
assertEquals(Yaml.pretty(openAPI.getComponents().getSchemas().get("test")), "{}\n");
}

@Test
public void testBooleanAdditionalPropertiesSerialization() throws Exception{
String expectedJson = "{\n" +
" \"openapi\" : \"3.1.0\",\n" +
" \"components\" : {\n" +
" \"schemas\" : {\n" +
" \"test\" : {\n" +
" \"type\" : \"object\",\n" +
" \"additionalProperties\" : true\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

String expectedYaml = "openapi: 3.1.0\n" +
"components:\n" +
" schemas:\n" +
" test:\n" +
" type: object\n" +
" additionalProperties: true\n";

OpenAPI openAPI = Json31.mapper().readValue(expectedJson, OpenAPI.class);
String ser = Json31.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
openAPI = Json.mapper().readValue(expectedJson, OpenAPI.class);
ser = Json.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));

openAPI = Yaml31.mapper().readValue(expectedYaml, OpenAPI.class);
ser = Yaml31.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
openAPI = Yaml.mapper().readValue(expectedYaml, OpenAPI.class);
ser = Yaml.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));

expectedJson = "{\n" +
" \"openapi\" : \"3.0.0\",\n" +
" \"components\" : {\n" +
" \"schemas\" : {\n" +
" \"test\" : {\n" +
" \"type\" : \"object\",\n" +
" \"additionalProperties\" : true\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

expectedYaml = "openapi: 3.0.0\n" +
"components:\n" +
" schemas:\n" +
" test:\n" +
" type: object\n" +
" additionalProperties: true\n";

openAPI = Json31.mapper().readValue(expectedJson, OpenAPI.class);
ser = Json31.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
openAPI = Json.mapper().readValue(expectedJson, OpenAPI.class);
ser = Json.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));

openAPI = Yaml31.mapper().readValue(expectedYaml, OpenAPI.class);
ser = Yaml31.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
openAPI = Yaml.mapper().readValue(expectedYaml, OpenAPI.class);
ser = Yaml.pretty(openAPI);
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
}

private static String withJacksonSystemLineSeparator(String s) {
return s.replace("\n", DefaultIndenter.SYS_LF);
}
Expand Down
4 changes: 2 additions & 2 deletions modules/swagger-eclipse-transformer-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.4</version>
<version>3.7.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -122,7 +122,7 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.2.1</version>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ static abstract class SortedSchemaMixin {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonInclude(JsonInclude.Include.CUSTOM)
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

@JsonIgnore
Expand Down Expand Up @@ -753,7 +753,7 @@ static abstract class SortedSchemaMixin31 {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonInclude(JsonInclude.Include.CUSTOM)
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static abstract class SortedSchemaMixin {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonInclude(JsonInclude.Include.CUSTOM)
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

@JsonIgnore
Expand Down
6 changes: 3 additions & 3 deletions modules/swagger-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.4</version>
<version>3.7.0</version>
</plugin>
<plugin>
<groupId>io.swagger.core.v3</groupId>
Expand Down Expand Up @@ -88,7 +88,7 @@
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.4</version>
<version>3.7.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -145,7 +145,7 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.2.1</version>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Expand Down
Loading

0 comments on commit 6905f74

Please sign in to comment.