Skip to content

Commit 57ed4d1

Browse files
committed
Fix bug the prevented generation of composite classes examples
1 parent 07eebee commit 57ed4d1

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/DefaultSchemasService.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.github.stavshamir.springwolf.schemas;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.fasterxml.jackson.databind.module.SimpleModule;
67
import io.swagger.oas.inflector.examples.ExampleBuilder;
78
import io.swagger.oas.inflector.examples.models.Example;
89
import io.swagger.oas.inflector.processors.JsonNodeExampleSerializer;
9-
import io.swagger.util.Json;
1010
import io.swagger.v3.core.converter.ModelConverters;
1111
import io.swagger.v3.oas.models.media.Schema;
1212
import lombok.extern.slf4j.Slf4j;
@@ -27,14 +27,18 @@ public class DefaultSchemasService implements SchemasService {
2727

2828
public DefaultSchemasService() {
2929
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
30-
3130
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
32-
Json.mapper().registerModule(simpleModule);
31+
objectMapper.registerModule(simpleModule);
3332
}
3433

3534
@Override
3635
public Map<String, Schema> getDefinitions() {
37-
definitions.forEach((k, schema) -> setExample(schema));
36+
// The examples must first be set as JSON strings (the inflector does not work otherwise)
37+
definitions.forEach(this::buildExampleAsString);
38+
39+
// Then they must be deserialized to map, or they will be serialized as reguler string and not json by the
40+
// object mapper
41+
definitions.forEach(this::deserializeExampleToMap);
3842
return definitions;
3943
}
4044

@@ -48,14 +52,21 @@ public String register(Class<?> type) {
4852
return type.getSimpleName();
4953
}
5054

51-
private void setExample(Schema schema) {
55+
private void buildExampleAsString(String k, Schema schema) {
5256
log.debug("Setting example for {}", schema.getName());
5357

5458
Example example = ExampleBuilder.fromSchema(schema, definitions);
55-
String exampleAsJson = Json.pretty(example);
59+
try {
60+
String exampleAsJson = objectMapper.writeValueAsString(example);
61+
schema.setExample(exampleAsJson);
62+
} catch (JsonProcessingException e) {
63+
log.error("Failed to write example value as a string");
64+
}
65+
}
5666

67+
private void deserializeExampleToMap(String k, Schema schema) {
5768
try {
58-
schema.setExample(objectMapper.readValue(exampleAsJson, Map.class));
69+
schema.setExample(objectMapper.readValue((String) schema.getExample(), Map.class));
5970
} catch (IOException e) {
6071
log.error("Failed to convert example object of {} to map", schema.getName());
6172
}

springwolf-example/src/main/java/io/github/stavshamir/springwolf/example/dtos/AnotherPayloadDto.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class AnotherPayloadDto {
44

55
private String foo;
6+
private ExamplePayloadDto example;
67

78
public String getFoo() {
89
return foo;
@@ -12,4 +13,20 @@ public void setFoo(String foo) {
1213
this.foo = foo;
1314
}
1415

16+
public ExamplePayloadDto getExample() {
17+
return example;
18+
}
19+
20+
public void setExample(ExamplePayloadDto example) {
21+
this.example = example;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "AnotherPayloadDto{" +
27+
"foo='" + foo + '\'' +
28+
", example=" + example +
29+
'}';
30+
}
31+
1532
}

springwolf-example/src/test/resources/asyncapi.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,18 @@
8686
"properties": {
8787
"foo": {
8888
"type": "string"
89+
},
90+
"example": {
91+
"$ref": "#/components/schemas/ExamplePayloadDto"
8992
}
9093
},
9194
"example": {
92-
"foo": "string"
95+
"foo": "string",
96+
"example": {
97+
"someString": "string",
98+
"someLong": 0,
99+
"someEnum": "FOO1"
100+
}
93101
}
94102
}
95103
}

0 commit comments

Comments
 (0)