Skip to content

Commit 81dc4b5

Browse files
timonbacksam0r040
andauthored
Refactor/improve example schema generator (#552)
* feat: log allowed payloadTypes to console * fix: handling of string schema Co-authored-by: sam0r040 <93372330+sam0r040@users.noreply.github.com> --------- Co-authored-by: sam0r040 <93372330+sam0r040@users.noreply.github.com>
1 parent 8c17314 commit 81dc4b5

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/controller/PublishingPayloadCreator.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import io.swagger.v3.oas.models.media.Schema;
99
import lombok.RequiredArgsConstructor;
1010
import lombok.extern.slf4j.Slf4j;
11+
import org.apache.commons.lang3.StringUtils;
1112

1213
import java.text.MessageFormat;
14+
import java.util.List;
1315

1416
/**
1517
* Used in plugins with publishing enabled.
@@ -23,12 +25,14 @@ public class PublishingPayloadCreator {
2325
private final ObjectMapper objectMapper;
2426

2527
public Result createPayloadObject(MessageDto message) {
26-
2728
String messagePayloadType = message.getPayloadType();
28-
for (Schema<?> value : schemasService.getDefinitions().values()) {
29-
String schemaPayloadType = value.getName();
29+
30+
List<String> knownSchemaNames = schemasService.getDefinitions().values().stream()
31+
.map(Schema::getName)
32+
.toList();
33+
for (String schemaPayloadType : knownSchemaNames) {
3034
// security: match against user input, but always use our controlled data from the DefaultSchemaService
31-
if (schemaPayloadType.equals(messagePayloadType)) {
35+
if (schemaPayloadType != null && schemaPayloadType.equals(messagePayloadType)) {
3236
try {
3337
Class<?> payloadClass = Class.forName(schemaPayloadType);
3438
Object payload = objectMapper.readValue(message.getPayload(), payloadClass);
@@ -44,7 +48,9 @@ public Result createPayloadObject(MessageDto message) {
4448

4549
String errorMessage = MessageFormat.format(
4650
"Specified payloadType {0} is not a registered springwolf schema.", messagePayloadType);
47-
log.info(errorMessage);
51+
String knownPayloadsMessage =
52+
MessageFormat.format(" Known payloadTypes: [{0}]", StringUtils.join(knownSchemaNames, ", "));
53+
log.info(errorMessage + knownPayloadsMessage);
4854
return new Result(null, errorMessage);
4955
}
5056

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private void processAsyncApiPayloadAnnotation(Map<String, Schema> schemas, Strin
122122
private String registerString() {
123123
String schemaName = "String";
124124
StringSchema schema = new StringSchema();
125+
schema.setName(String.class.getName());
125126

126127
this.definitions.put(schemaName, schema);
127128
postProcessSchema(schema);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ private static String DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(String format) {
5656
@Override
5757
public Object fromSchema(Schema schema, Map<String, Schema> definitions) {
5858
try {
59-
String exampleString = buildSchema(schema, definitions);
60-
return objectMapper.readValue(exampleString, Object.class);
61-
} catch (JsonProcessingException | ExampleGeneratingException ex) {
59+
return buildSchemaInternal(schema, definitions, new HashSet<>());
60+
} catch (ExampleGeneratingException ex) {
6261
log.info("Failed to build json example for schema {}", schema.getName(), ex);
6362
}
6463
return null;
@@ -104,6 +103,11 @@ private static JsonNode getExampleValueFromSchemaAnnotation(Schema schema) {
104103
return null;
105104
}
106105

106+
// Return directly, when we have processed this before
107+
if (exampleValue instanceof JsonNode) {
108+
return (JsonNode) exampleValue;
109+
}
110+
107111
// Create an ObjectNode to hold the example JSON
108112
JsonNode exampleNode = objectMapper.createObjectNode();
109113

springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void build() {
3636

3737
Object actual = generator.fromSchema(schema, emptyMap());
3838

39-
assertThat(actual).isEqualTo("string");
39+
assertThat(actual.toString()).isEqualTo("\"string\"");
4040
}
4141

4242
@Test

springwolf-core/src/test/resources/schemas/api-payload.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"maxLength" : 10,
44
"type" : "string",
55
"description" : "The payload in the envelop",
6-
"example" : "string"
6+
"example" : "\"string\""
77
}
88
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,10 @@
564564
},
565565
"String": {
566566
"type": "string",
567-
"example": "string",
567+
"example": "\"string\"",
568568
"x-json-schema": {
569569
"$schema": "https://json-schema.org/draft-04/schema#",
570+
"name": "java.lang.String",
570571
"type": "string"
571572
}
572573
},
@@ -606,7 +607,7 @@
606607
"maxLength": 100,
607608
"type": "string",
608609
"description": "Payload description using @Schema annotation and @AsyncApiPayload within envelope class",
609-
"example": "string",
610+
"example": "\"string\"",
610611
"x-json-schema": {
611612
"$schema": "https://json-schema.org/draft-04/schema#",
612613
"description": "Payload description using @Schema annotation and @AsyncApiPayload within envelope class",

0 commit comments

Comments
 (0)