mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-18 05:39:21 +00:00
Make yaml serialization deterministic (#233)
This commit is contained in:
@@ -22,7 +22,6 @@ import com.google.common.base.CaseFormat;
|
||||
import com.samskivert.mustache.Mustache.Compiler;
|
||||
|
||||
import io.swagger.v3.core.util.Json;
|
||||
import io.swagger.v3.core.util.Yaml;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.oas.models.headers.Header;
|
||||
@@ -47,6 +46,7 @@ import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openapitools.codegen.examples.ExampleGenerator;
|
||||
import org.openapitools.codegen.serializer.SerializerUtils;
|
||||
import org.openapitools.codegen.utils.ModelUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -4336,12 +4336,9 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
*/
|
||||
public void generateYAMLSpecFile(Map<String, Object> objs) {
|
||||
OpenAPI openAPI = (OpenAPI) objs.get("openAPI");
|
||||
if (openAPI != null) {
|
||||
try {
|
||||
objs.put("openapi-yaml", Yaml.mapper().writeValueAsString(openAPI));
|
||||
} catch (JsonProcessingException e) {
|
||||
LOGGER.error(e.getMessage(), e);
|
||||
}
|
||||
String yaml = SerializerUtils.toYamlString(openAPI);
|
||||
if(yaml != null) {
|
||||
objs.put("openapi-yaml", yaml);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.openapitools.codegen.serializer;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.openapitools.codegen.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
import io.swagger.v3.core.util.Yaml;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SerializerUtils {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SerializerUtils.class);
|
||||
|
||||
public static String toYamlString(OpenAPI openAPI) {
|
||||
if(openAPI == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
SimpleModule module = new SimpleModule("OpenAPIModule");
|
||||
module.addSerializer(OpenAPI.class, new OpenAPISerializer());
|
||||
try {
|
||||
return Yaml.mapper()
|
||||
.registerModule(module)
|
||||
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
|
||||
.writeValueAsString(openAPI)
|
||||
.replace("\r\n", "\n");
|
||||
} catch (JsonProcessingException e) {
|
||||
LOGGER.warn("Can not create yaml content", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user