Skip to content

Commit 6905f74

Browse files
committed
deps update and adjust example (de)serialization
1 parent 98a3e70 commit 6905f74

File tree

12 files changed

+265
-27
lines changed

12 files changed

+265
-27
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.swagger.v3.core.jackson;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.databind.JsonSerializer;
6+
import com.fasterxml.jackson.databind.SerializerProvider;
7+
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
8+
import io.swagger.v3.oas.models.examples.Example;
9+
10+
import java.io.IOException;
11+
12+
public class ExampleSerializer extends JsonSerializer<Example> implements ResolvableSerializer {
13+
14+
private JsonSerializer<Object> defaultSerializer;
15+
16+
public ExampleSerializer(JsonSerializer<Object> serializer) {
17+
defaultSerializer = serializer;
18+
}
19+
20+
@Override
21+
public void resolve(SerializerProvider serializerProvider) throws JsonMappingException {
22+
if (defaultSerializer instanceof ResolvableSerializer) {
23+
((ResolvableSerializer) defaultSerializer).resolve(serializerProvider);
24+
}
25+
}
26+
27+
@Override
28+
public void serialize(
29+
Example example, JsonGenerator jgen, SerializerProvider provider)
30+
throws IOException {
31+
32+
if (example.getValueSetFlag() && example.getValue() == null) {
33+
jgen.writeStartObject();
34+
defaultSerializer.unwrappingSerializer(null).serialize(example, jgen, provider);
35+
jgen.writeNullField("value");
36+
jgen.writeEndObject();
37+
} else {
38+
defaultSerializer.serialize(example, jgen, provider);
39+
}
40+
}
41+
}

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/ExampleMixin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
import java.util.Map;
99

1010
public abstract class ExampleMixin {
11-
1211
@JsonAnyGetter
1312
public abstract Map<String, Object> getExtensions();
1413

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

18-
@JsonInclude(JsonInclude.Include.CUSTOM)
17+
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
1918
public abstract Object getValue();
2019

2120
@JsonIgnore

modules/swagger-core/src/main/java/io/swagger/v3/core/util/ObjectMapperFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
1515
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
1616
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
17+
import io.swagger.v3.core.jackson.ExampleSerializer;
1718
import io.swagger.v3.core.jackson.Schema31Serializer;
1819
import io.swagger.v3.core.jackson.MediaTypeSerializer;
1920
import io.swagger.v3.core.jackson.SchemaSerializer;
@@ -116,6 +117,8 @@ public JsonSerializer<?> modifySerializer(
116117
return new SchemaSerializer((JsonSerializer<Object>) serializer);
117118
} else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
118119
return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
120+
} else if (Example.class.isAssignableFrom(desc.getBeanClass())) {
121+
return new ExampleSerializer((JsonSerializer<Object>) serializer);
119122
}
120123
return serializer;
121124
}
@@ -135,6 +138,8 @@ public JsonSerializer<?> modifySerializer(
135138
return new Schema31Serializer((JsonSerializer<Object>) serializer);
136139
} else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
137140
return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
141+
} else if (Example.class.isAssignableFrom(desc.getBeanClass())) {
142+
return new ExampleSerializer((JsonSerializer<Object>) serializer);
138143
}
139144
return serializer;
140145
}

modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.swagger.v3.core.util.Yaml;
99
import io.swagger.v3.oas.models.OpenAPI;
1010
import io.swagger.v3.oas.models.PathItem;
11+
import io.swagger.v3.oas.models.examples.Example;
1112
import io.swagger.v3.oas.models.headers.Header;
1213
import io.swagger.v3.oas.models.media.ComposedSchema;
1314
import io.swagger.v3.oas.models.media.Encoding;
@@ -411,6 +412,120 @@ public void testNullExampleDeserialization() throws Exception {
411412
Yaml.prettyPrint(oas);
412413
}
413414

415+
@Test
416+
public void testNullExampleAndValues() throws Exception {
417+
String yamlNull = "openapi: 3.0.1\n" +
418+
"paths:\n" +
419+
" /:\n" +
420+
" get:\n" +
421+
" description: Operation Description\n" +
422+
" operationId: operationId\n" +
423+
"components:\n" +
424+
" schemas:\n" +
425+
" UserStatus:\n" +
426+
" type: object\n" +
427+
" example: null\n";
428+
429+
String yamlMissing = "openapi: 3.0.1\n" +
430+
"paths:\n" +
431+
" /:\n" +
432+
" get:\n" +
433+
" description: Operation Description\n" +
434+
" operationId: operationId\n" +
435+
"components:\n" +
436+
" schemas:\n" +
437+
" UserStatus:\n" +
438+
" type: object\n";
439+
440+
String yamlNotNull = "openapi: 3.0.1\n" +
441+
"paths:\n" +
442+
" /:\n" +
443+
" get:\n" +
444+
" description: Operation Description\n" +
445+
" operationId: operationId\n" +
446+
"components:\n" +
447+
" schemas:\n" +
448+
" UserStatus:\n" +
449+
" type: object\n" +
450+
" example:\n" +
451+
" value: bar\n";
452+
453+
String yamlValueNull = "openapi: 3.0.1\n" +
454+
"paths:\n" +
455+
" /:\n" +
456+
" get:\n" +
457+
" description: Operation Description\n" +
458+
" operationId: operationId\n" +
459+
"components:\n" +
460+
" examples:\n" +
461+
" UserStatus:\n" +
462+
" summary: string\n" +
463+
" value: null\n";
464+
465+
String yamlValueMissing = "openapi: 3.0.1\n" +
466+
"paths:\n" +
467+
" /:\n" +
468+
" get:\n" +
469+
" description: Operation Description\n" +
470+
" operationId: operationId\n" +
471+
"components:\n" +
472+
" examples:\n" +
473+
" UserStatus:\n" +
474+
" summary: string\n";
475+
476+
String yamlValueNotNull = "openapi: 3.0.1\n" +
477+
"paths:\n" +
478+
" /:\n" +
479+
" get:\n" +
480+
" description: Operation Description\n" +
481+
" operationId: operationId\n" +
482+
"components:\n" +
483+
" examples:\n" +
484+
" UserStatus:\n" +
485+
" summary: string\n" +
486+
" value: bar\n";
487+
488+
OpenAPI oas = Yaml.mapper().readValue(yamlNull, OpenAPI.class);
489+
Yaml.prettyPrint(oas);
490+
491+
assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
492+
assertTrue(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
493+
assertEquals(Yaml.pretty(oas), yamlNull);
494+
495+
oas = Yaml.mapper().readValue(yamlMissing, OpenAPI.class);
496+
Yaml.prettyPrint(oas);
497+
assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
498+
assertFalse(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
499+
assertEquals(Yaml.pretty(oas), yamlMissing);
500+
501+
oas = Yaml.mapper().readValue(yamlNotNull, OpenAPI.class);
502+
Yaml.prettyPrint(oas);
503+
assertNotNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
504+
assertTrue(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
505+
assertEquals(Yaml.pretty(oas), yamlNotNull);
506+
507+
oas = Yaml.mapper().readValue(yamlValueNull, OpenAPI.class);
508+
Yaml.prettyPrint(oas);
509+
Example ex = oas.getComponents().getExamples().get("UserStatus");
510+
assertNull(ex.getValue());
511+
assertTrue(ex.getValueSetFlag());
512+
assertEquals(Yaml.pretty(oas), yamlValueNull);
513+
514+
oas = Yaml.mapper().readValue(yamlValueMissing, OpenAPI.class);
515+
Yaml.prettyPrint(oas);
516+
ex = oas.getComponents().getExamples().get("UserStatus");
517+
assertNull(ex.getValue());
518+
assertFalse(ex.getValueSetFlag());
519+
assertEquals(Yaml.pretty(oas), yamlValueMissing);
520+
521+
oas = Yaml.mapper().readValue(yamlValueNotNull, OpenAPI.class);
522+
Yaml.prettyPrint(oas);
523+
ex = oas.getComponents().getExamples().get("UserStatus");
524+
assertNotNull(ex.getValue());
525+
assertTrue(ex.getValueSetFlag());
526+
assertEquals(Yaml.pretty(oas), yamlValueNotNull);
527+
}
528+
414529
@Test
415530
public void testExampleDeserializationOnMediaType() throws Exception {
416531
String content = FileUtils.readFileToString(new File("src/test/resources/specFiles/media-type-null-example.yaml"), "UTF-8");

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static org.testng.Assert.assertEquals;
3535
import static org.testng.Assert.assertNotNull;
36+
import static org.testng.Assert.assertTrue;
3637

3738
public class OpenAPI3_1SerializationTest {
3839

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

1423+
@Test
1424+
public void testBooleanAdditionalPropertiesSerialization() throws Exception{
1425+
String expectedJson = "{\n" +
1426+
" \"openapi\" : \"3.1.0\",\n" +
1427+
" \"components\" : {\n" +
1428+
" \"schemas\" : {\n" +
1429+
" \"test\" : {\n" +
1430+
" \"type\" : \"object\",\n" +
1431+
" \"additionalProperties\" : true\n" +
1432+
" }\n" +
1433+
" }\n" +
1434+
" }\n" +
1435+
"}";
1436+
1437+
String expectedYaml = "openapi: 3.1.0\n" +
1438+
"components:\n" +
1439+
" schemas:\n" +
1440+
" test:\n" +
1441+
" type: object\n" +
1442+
" additionalProperties: true\n";
1443+
1444+
OpenAPI openAPI = Json31.mapper().readValue(expectedJson, OpenAPI.class);
1445+
String ser = Json31.pretty(openAPI);
1446+
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
1447+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1448+
openAPI = Json.mapper().readValue(expectedJson, OpenAPI.class);
1449+
ser = Json.pretty(openAPI);
1450+
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
1451+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1452+
1453+
openAPI = Yaml31.mapper().readValue(expectedYaml, OpenAPI.class);
1454+
ser = Yaml31.pretty(openAPI);
1455+
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
1456+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1457+
openAPI = Yaml.mapper().readValue(expectedYaml, OpenAPI.class);
1458+
ser = Yaml.pretty(openAPI);
1459+
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
1460+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1461+
1462+
expectedJson = "{\n" +
1463+
" \"openapi\" : \"3.0.0\",\n" +
1464+
" \"components\" : {\n" +
1465+
" \"schemas\" : {\n" +
1466+
" \"test\" : {\n" +
1467+
" \"type\" : \"object\",\n" +
1468+
" \"additionalProperties\" : true\n" +
1469+
" }\n" +
1470+
" }\n" +
1471+
" }\n" +
1472+
"}";
1473+
1474+
expectedYaml = "openapi: 3.0.0\n" +
1475+
"components:\n" +
1476+
" schemas:\n" +
1477+
" test:\n" +
1478+
" type: object\n" +
1479+
" additionalProperties: true\n";
1480+
1481+
openAPI = Json31.mapper().readValue(expectedJson, OpenAPI.class);
1482+
ser = Json31.pretty(openAPI);
1483+
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
1484+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1485+
openAPI = Json.mapper().readValue(expectedJson, OpenAPI.class);
1486+
ser = Json.pretty(openAPI);
1487+
assertEquals(ser, withJacksonSystemLineSeparator(expectedJson));
1488+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1489+
1490+
openAPI = Yaml31.mapper().readValue(expectedYaml, OpenAPI.class);
1491+
ser = Yaml31.pretty(openAPI);
1492+
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
1493+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1494+
openAPI = Yaml.mapper().readValue(expectedYaml, OpenAPI.class);
1495+
ser = Yaml.pretty(openAPI);
1496+
assertEquals(ser, withJacksonSystemLineSeparator(expectedYaml));
1497+
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
1498+
}
1499+
14221500
private static String withJacksonSystemLineSeparator(String s) {
14231501
return s.replace("\n", DefaultIndenter.SYS_LF);
14241502
}

modules/swagger-eclipse-transformer-maven-plugin/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
<dependency>
9595
<groupId>org.apache.maven.plugin-tools</groupId>
9696
<artifactId>maven-plugin-annotations</artifactId>
97-
<version>3.6.4</version>
97+
<version>3.7.0</version>
9898
<scope>provided</scope>
9999
<exclusions>
100100
<exclusion>
@@ -122,7 +122,7 @@
122122
<dependency>
123123
<groupId>org.codehaus.plexus</groupId>
124124
<artifactId>plexus-utils</artifactId>
125-
<version>3.2.1</version>
125+
<version>3.5.0</version>
126126
</dependency>
127127
<dependency>
128128
<groupId>org.codehaus.plexus</groupId>

modules/swagger-integration/src/main/java/io/swagger/v3/oas/integration/GenericOpenApiContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ static abstract class SortedSchemaMixin {
670670
@JsonIgnore
671671
public abstract boolean getExampleSetFlag();
672672

673-
@JsonInclude(JsonInclude.Include.CUSTOM)
673+
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
674674
public abstract Object getExample();
675675

676676
@JsonIgnore
@@ -753,7 +753,7 @@ static abstract class SortedSchemaMixin31 {
753753
@JsonIgnore
754754
public abstract boolean getExampleSetFlag();
755755

756-
@JsonInclude(JsonInclude.Include.CUSTOM)
756+
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
757757
public abstract Object getExample();
758758

759759
@JsonIgnore

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/integration/SortedOutputTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static abstract class SortedSchemaMixin {
114114
@JsonIgnore
115115
public abstract boolean getExampleSetFlag();
116116

117-
@JsonInclude(JsonInclude.Include.CUSTOM)
117+
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
118118
public abstract Object getExample();
119119

120120
@JsonIgnore

modules/swagger-maven-plugin/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<plugin>
3030
<groupId>org.apache.maven.plugins</groupId>
3131
<artifactId>maven-plugin-plugin</artifactId>
32-
<version>3.6.4</version>
32+
<version>3.7.0</version>
3333
</plugin>
3434
<plugin>
3535
<groupId>io.swagger.core.v3</groupId>
@@ -88,7 +88,7 @@
8888
<dependency>
8989
<groupId>org.apache.maven.plugin-tools</groupId>
9090
<artifactId>maven-plugin-annotations</artifactId>
91-
<version>3.6.4</version>
91+
<version>3.7.0</version>
9292
<scope>provided</scope>
9393
<exclusions>
9494
<exclusion>
@@ -145,7 +145,7 @@
145145
<dependency>
146146
<groupId>org.codehaus.plexus</groupId>
147147
<artifactId>plexus-utils</artifactId>
148-
<version>3.2.1</version>
148+
<version>3.5.0</version>
149149
</dependency>
150150
<dependency>
151151
<groupId>org.codehaus.plexus</groupId>

0 commit comments

Comments
 (0)