Skip to content

Commit

Permalink
Refactor/improve example schema generator (#552)
Browse files Browse the repository at this point in the history
* feat: log allowed payloadTypes to console

* fix: handling of string schema

Co-authored-by: sam0r040 <[email protected]>

---------

Co-authored-by: sam0r040 <[email protected]>
  • Loading branch information
timonback and sam0r040 authored Jan 19, 2024
1 parent 8c17314 commit 81dc4b5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import io.swagger.v3.oas.models.media.Schema;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.List;

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

public Result createPayloadObject(MessageDto message) {

String messagePayloadType = message.getPayloadType();
for (Schema<?> value : schemasService.getDefinitions().values()) {
String schemaPayloadType = value.getName();

List<String> knownSchemaNames = schemasService.getDefinitions().values().stream()
.map(Schema::getName)
.toList();
for (String schemaPayloadType : knownSchemaNames) {
// security: match against user input, but always use our controlled data from the DefaultSchemaService
if (schemaPayloadType.equals(messagePayloadType)) {
if (schemaPayloadType != null && schemaPayloadType.equals(messagePayloadType)) {
try {
Class<?> payloadClass = Class.forName(schemaPayloadType);
Object payload = objectMapper.readValue(message.getPayload(), payloadClass);
Expand All @@ -44,7 +48,9 @@ public Result createPayloadObject(MessageDto message) {

String errorMessage = MessageFormat.format(
"Specified payloadType {0} is not a registered springwolf schema.", messagePayloadType);
log.info(errorMessage);
String knownPayloadsMessage =
MessageFormat.format(" Known payloadTypes: [{0}]", StringUtils.join(knownSchemaNames, ", "));
log.info(errorMessage + knownPayloadsMessage);
return new Result(null, errorMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private void processAsyncApiPayloadAnnotation(Map<String, Schema> schemas, Strin
private String registerString() {
String schemaName = "String";
StringSchema schema = new StringSchema();
schema.setName(String.class.getName());

this.definitions.put(schemaName, schema);
postProcessSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ private static String DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(String format) {
@Override
public Object fromSchema(Schema schema, Map<String, Schema> definitions) {
try {
String exampleString = buildSchema(schema, definitions);
return objectMapper.readValue(exampleString, Object.class);
} catch (JsonProcessingException | ExampleGeneratingException ex) {
return buildSchemaInternal(schema, definitions, new HashSet<>());
} catch (ExampleGeneratingException ex) {
log.info("Failed to build json example for schema {}", schema.getName(), ex);
}
return null;
Expand Down Expand Up @@ -104,6 +103,11 @@ private static JsonNode getExampleValueFromSchemaAnnotation(Schema schema) {
return null;
}

// Return directly, when we have processed this before
if (exampleValue instanceof JsonNode) {
return (JsonNode) exampleValue;
}

// Create an ObjectNode to hold the example JSON
JsonNode exampleNode = objectMapper.createObjectNode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void build() {

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

assertThat(actual).isEqualTo("string");
assertThat(actual.toString()).isEqualTo("\"string\"");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"maxLength" : 10,
"type" : "string",
"description" : "The payload in the envelop",
"example" : "string"
"example" : "\"string\""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,10 @@
},
"String": {
"type": "string",
"example": "string",
"example": "\"string\"",
"x-json-schema": {
"$schema": "https://json-schema.org/draft-04/schema#",
"name": "java.lang.String",
"type": "string"
}
},
Expand Down Expand Up @@ -606,7 +607,7 @@
"maxLength": 100,
"type": "string",
"description": "Payload description using @Schema annotation and @AsyncApiPayload within envelope class",
"example": "string",
"example": "\"string\"",
"x-json-schema": {
"$schema": "https://json-schema.org/draft-04/schema#",
"description": "Payload description using @Schema annotation and @AsyncApiPayload within envelope class",
Expand Down

0 comments on commit 81dc4b5

Please sign in to comment.