Skip to content

Commit

Permalink
fix: Fixed JSON serializer to not leak unintended values
Browse files Browse the repository at this point in the history
Fixes springwolf#169

The `exampleSetFlag` and `types` properties belong to `swagger-core` internals and are not part of the AsyncAPI specification.

See swagger-api/swagger-core#3637 (comment) for some details.

This PR replaces the custom JSON ObjectMapper by the `swagger-core` one, which already contains all the needed `mixins` to properly serialize the specification.

Fixes springwolf#169 for Json Serialization
  • Loading branch information
Carlos Tasada committed May 12, 2023
1 parent 52b8e2a commit 6d53276
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.github.stavshamir.springwolf.asyncapi;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import io.github.stavshamir.springwolf.asyncapi.types.AsyncAPI;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;
Expand All @@ -18,20 +17,12 @@
@Service
public class DefaultAsyncApiSerializerService implements AsyncApiSerializerService {

private ObjectMapper jsonMapper = new ObjectMapper();

private ObjectMapper jsonMapper = Json.mapper();
private ObjectMapper yamlMapper = Yaml.mapper();
private PrettyPrinter printer = new DefaultPrettyPrinter().withObjectIndenter(new DefaultIndenter(" ", DefaultIndenter.SYS_LF));

@PostConstruct
void postConstruct() {
jsonMapper.setConfig(
jsonMapper.getSerializationConfig()
.with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
.without(SerializationFeature.FAIL_ON_EMPTY_BEANS)
);
jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

((YAMLFactory)yamlMapper.getFactory()).enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.oas.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
Expand All @@ -24,7 +25,7 @@
public class DefaultSchemasService implements SchemasService {

private final ModelConverters converter = ModelConverters.getInstance();
private final ObjectMapper objectMapper = new ObjectMapper();
private final ObjectMapper objectMapper = Json.mapper();

private final Map<String, Schema> definitions = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -24,7 +25,7 @@ public class DefaultSchemasServiceTest {
private final SchemasService schemasService = new DefaultSchemasService(Optional.empty());

private static final String EXAMPLES_PATH = "/schemas/examples";
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper = Json.mapper();

static {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Expand Down
11 changes: 3 additions & 8 deletions springwolf-core/src/test/resources/asyncapi/asyncapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
"bindings" : {
"kafka" : {
"key" : {
"type" : "string",
"exampleSetFlag" : false,
"types" : [ "string" ]
"type" : "string"
},
"bindingVersion" : "binding-version-1"
}
Expand All @@ -66,12 +64,9 @@
"type" : "object",
"properties" : {
"s" : {
"type" : "string",
"exampleSetFlag" : false,
"types" : [ "string" ]
"type" : "string"
}
},
"exampleSetFlag" : false
}
}
}
},
Expand Down
40 changes: 8 additions & 32 deletions springwolf-core/src/test/resources/schemas/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
"type": "object",
"properties": {
"s": {
"type": "string",
"exampleSetFlag": false,
"types": [
"string"
]
"type": "string"
},
"f": {
"$ref": "#/components/schemas/SimpleFoo",
"exampleSetFlag": false
"$ref": "#/components/schemas/SimpleFoo"
}
},
"example": {
Expand All @@ -20,25 +15,16 @@
"s": "string",
"b": true
}
},
"exampleSetFlag": true
}
},
"FooWithEnum": {
"type": "object",
"properties": {
"s": {
"type": "string",
"exampleSetFlag": false,
"types": [
"string"
]
"type": "string"
},
"b": {
"type": "string",
"exampleSetFlag": false,
"types": [
"string"
],
"enum": [
"BAR1",
"BAR2"
Expand All @@ -48,31 +34,21 @@
"example": {
"s": "string",
"b": "BAR1"
},
"exampleSetFlag": true
}
},
"SimpleFoo": {
"type": "object",
"properties": {
"s": {
"type": "string",
"exampleSetFlag": false,
"types": [
"string"
]
"type": "string"
},
"b": {
"type": "boolean",
"exampleSetFlag": false,
"types": [
"boolean"
]
"type": "boolean"
}
},
"example": {
"s": "string",
"b": true
},
"exampleSetFlag": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
"s": {
"description": "s field",
"type": "string",
"example": "s value",
"exampleSetFlag": true,
"types": [
"string"
]
"example": "s value"
}
},
"example": {
"s": "s value"
},
"exampleSetFlag": true,
"required": [
"s"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,12 @@
"type" : "object",
"properties" : {
"example" : {
"$ref" : "#/components/schemas/ExamplePayloadDto",
"exampleSetFlag" : false
"$ref" : "#/components/schemas/ExamplePayloadDto"
},
"foo" : {
"type" : "string",
"description" : "Foo field",
"example" : "bar",
"exampleSetFlag" : true,
"types" : [ "string" ]
"example" : "bar"
}
},
"description" : "Another payload model",
Expand All @@ -312,8 +309,7 @@
"someString" : "some string value"
},
"foo" : "bar"
},
"exampleSetFlag" : true
}
},
"ExamplePayloadDto" : {
"required" : [ "someEnum", "someString" ],
Expand All @@ -323,40 +319,31 @@
"type" : "string",
"description" : "Some enum field",
"example" : "FOO2",
"exampleSetFlag" : true,
"types" : [ "string" ],
"enum" : [ "FOO1", "FOO2", "FOO3" ]
},
"someLong" : {
"type" : "integer",
"description" : "Some long field",
"format" : "int64",
"example" : 5,
"exampleSetFlag" : true,
"types" : [ "integer" ]
"example" : 5
},
"someString" : {
"type" : "string",
"description" : "Some string field",
"example" : "some string value",
"exampleSetFlag" : true,
"types" : [ "string" ]
"example" : "some string value"
}
},
"description" : "Example payload model",
"example" : {
"someEnum" : "FOO2",
"someLong" : 5,
"someString" : "some string value"
},
"exampleSetFlag" : true
}
},
"HeadersNotDocumented" : {
"type" : "object",
"properties" : { },
"example" : { },
"exampleSetFlag" : true,
"types" : [ "object" ]
"example" : { }
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,19 @@
"properties": {
"payloadString": {
"type": "string",
"exampleSetFlag": false,
"types": [
"string"
]
}
},
"example": {
"payloadString": "string"
},
"exampleSetFlag": true
}
},
"HeadersNotDocumented": {
"type": "object",
"properties": {},
"example": {},
"exampleSetFlag": true,
"types": [
"object"
]
Expand All @@ -122,10 +119,8 @@
"properties": {
"foo": {
"type": "array",
"exampleSetFlag": false,
"items": {
"type": "string",
"exampleSetFlag": false,
"types": [
"string"
]
Expand All @@ -137,7 +132,6 @@
"bar": {
"type": "integer",
"format": "int32",
"exampleSetFlag": false,
"types": [
"integer"
]
Expand All @@ -149,8 +143,7 @@
"string"
],
"bar": 0
},
"exampleSetFlag": true
}
},
"OutputPayload": {
"required": [
Expand All @@ -162,7 +155,6 @@
"type": "string",
"description": "Some string field",
"example": "some string value",
"exampleSetFlag": true,
"types": [
"string"
]
Expand All @@ -172,7 +164,6 @@
"description": "Some long field",
"format": "int64",
"example": 5,
"exampleSetFlag": true,
"types": [
"integer"
]
Expand All @@ -182,8 +173,7 @@
"example": {
"someString": "some string value",
"someLong": 5
},
"exampleSetFlag": true
}
}
}
},
Expand Down

0 comments on commit 6d53276

Please sign in to comment.