-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialisation of OpenAPI is not stable #2828
Comments
Work around until this is solved: I am using Yaml.mapper()
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.writeValueAsString(openAPI); And because this is not suitable for the OpenAPI instance (the root object) itself, an import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.swagger.v3.oas.models.OpenAPI;
import java.io.IOException;
import java.util.Map.Entry;
public class OpenAPISerializer extends JsonSerializer<OpenAPI> {
@Override
public void serialize(OpenAPI value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value != null) {
gen.writeStartObject();
gen.writeStringField("openapi", value.getOpenapi());
if(value.getInfo() != null) {
gen.writeObjectField("info", value.getInfo());
}
if(value.getExternalDocs() != null) {
gen.writeObjectField("externalDocs", value.getExternalDocs());
}
if(value.getServers() != null) {
gen.writeObjectField("servers", value.getServers());
}
if(value.getSecurity() != null) {
gen.writeObjectField("security", value.getSecurity());
}
if(value.getTags() != null) {
gen.writeObjectField("tags", value.getTags());
}
if(value.getPaths() != null) {
gen.writeObjectField("paths", value.getPaths());
}
if(value.getComponents() != null) {
gen.writeObjectField("components", value.getComponents());
}
if(value.getExtensions() != null) {
for (Entry<String, Object> e : value.getExtensions().entrySet()) {
gen.writeObjectField(e.getKey(), e.getValue());
}
}
gen.writeEndObject();
}
}
} Like this: SimpleModule module = new SimpleModule("OpenAPIModule");
module.addSerializer(OpenAPI.class, new OpenAPISerializer());
Yaml.mapper()
.registerModule(module)
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.writeValueAsString(openAPI); I think a solution for stable serialization should be provided in the core. Any feedback? |
current result with sorting enabled:
|
turns out when using gradle plugin with using a custom |
as long as the solution is not alpha sorting @jmini. model structure / order is important and can't just be discarded. |
FYI: |
A solution has been implemented in #3740, see PR comment for details and usage scenarios. Closing this ticket, any further issues in this area should be reported in a new or not closed ticket. |
moved from swagger-api/swagger-parser/issues/728 reported by @jmini
We have observed a strange behaviour with the Serialisation of an OpenAPI instance with
Yaml.mapper().writeValueAsString(openAPI)
produces different results:From a semantic point of view, the content is the same. But my expectation is to get the content always serialised the same way.
I did not understood what is producing the difference.
Maybe it depends from the JVM.
In my opinion
io.swagger.v3.core.jackson.SchemaSerializer
could and should be improved. It seems to me that thedefaultSerializer
is not working as expected.I am open for suggestions.
The text was updated successfully, but these errors were encountered: