forked from loafle/openapi-generator-original
Make yaml serialization deterministic (#233)
This commit is contained in:
parent
d649c1311e
commit
69a3852ef7
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package org.openapitools.codegen.serializer;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.ExternalDocumentation;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.oas.models.PathItem;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.media.ObjectSchema;
|
||||
import io.swagger.v3.oas.models.media.StringSchema;
|
||||
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.models.responses.ApiResponses;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.servers.Server;
|
||||
import io.swagger.v3.oas.models.tags.Tag;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SerializerUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testToYamlStringCompleteExample() throws Exception {
|
||||
OpenAPI openAPI = new OpenAPI();
|
||||
openAPI.setInfo(new Info().title("Some title").description("Some description"));
|
||||
openAPI.setExternalDocs(new ExternalDocumentation().url("http://abcdef.com").description("a-description"));
|
||||
openAPI.setServers(Arrays.asList(
|
||||
new Server().url("http://www.server1.com").description("first server"),
|
||||
new Server().url("http://www.server2.com").description("second server")
|
||||
));
|
||||
openAPI.setSecurity(Arrays.asList(
|
||||
new SecurityRequirement().addList("some_auth", Arrays.asList("write", "read"))
|
||||
));
|
||||
openAPI.setTags(Arrays.asList(
|
||||
new Tag().name("tag1").description("some 1 description"),
|
||||
new Tag().name("tag2").description("some 2 description"),
|
||||
new Tag().name("tag3").description("some 3 description")
|
||||
));
|
||||
openAPI.path("/ping/pong", new PathItem().get(new Operation()
|
||||
.description("Some description")
|
||||
.operationId("pingOp")
|
||||
.responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("Ok")))));
|
||||
openAPI.components(new Components().addSchemas("SomeObject", new ObjectSchema().description("An Obj").addProperties("id", new StringSchema())));
|
||||
openAPI.addExtension("x-custom", "value1");
|
||||
openAPI.addExtension("x-other", "value2");
|
||||
|
||||
String content = SerializerUtils.toYamlString(openAPI);
|
||||
String expected = "openapi: 3.0.1\n" +
|
||||
"info:\n" +
|
||||
" description: Some description\n" +
|
||||
" title: Some title\n" +
|
||||
"externalDocs:\n" +
|
||||
" description: a-description\n" +
|
||||
" url: http://abcdef.com\n" +
|
||||
"servers:\n" +
|
||||
"- description: first server\n" +
|
||||
" url: http://www.server1.com\n" +
|
||||
"- description: second server\n" +
|
||||
" url: http://www.server2.com\n" +
|
||||
"security:\n" +
|
||||
"- some_auth:\n" +
|
||||
" - write\n" +
|
||||
" - read\n" +
|
||||
"tags:\n" +
|
||||
"- description: some 1 description\n" +
|
||||
" name: tag1\n" +
|
||||
"- description: some 2 description\n" +
|
||||
" name: tag2\n" +
|
||||
"- description: some 3 description\n" +
|
||||
" name: tag3\n" +
|
||||
"paths:\n" +
|
||||
" /ping/pong:\n" +
|
||||
" get:\n" +
|
||||
" description: Some description\n" +
|
||||
" operationId: pingOp\n" +
|
||||
" responses:\n" +
|
||||
" 200:\n" +
|
||||
" description: Ok\n" +
|
||||
"components:\n" +
|
||||
" schemas:\n" +
|
||||
" SomeObject:\n" +
|
||||
" description: An Obj\n" +
|
||||
" properties:\n" +
|
||||
" id:\n" +
|
||||
" type: string\n" +
|
||||
" type: object\n" +
|
||||
"x-other: value2\n" +
|
||||
"x-custom: value1\n";
|
||||
assertEquals(content, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToYamlStringMinimalExample() throws Exception {
|
||||
OpenAPI openAPI = new OpenAPI();
|
||||
openAPI.setInfo(new Info().title("Some title"));
|
||||
openAPI.setServers(Arrays.asList(
|
||||
new Server().url("http://www.server1.com")
|
||||
));
|
||||
openAPI.path("/ping/pong", new PathItem().get(new Operation()
|
||||
.description("Some description")
|
||||
.operationId("pingOp")
|
||||
.responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("Ok")))));
|
||||
|
||||
String content = SerializerUtils.toYamlString(openAPI);
|
||||
String expected = "openapi: 3.0.1\n" +
|
||||
"info:\n" +
|
||||
" title: Some title\n" +
|
||||
"servers:\n" +
|
||||
"- url: http://www.server1.com\n" +
|
||||
"paths:\n" +
|
||||
" /ping/pong:\n" +
|
||||
" get:\n" +
|
||||
" description: Some description\n" +
|
||||
" operationId: pingOp\n" +
|
||||
" responses:\n" +
|
||||
" 200:\n" +
|
||||
" description: Ok\n";
|
||||
assertEquals(content, expected);
|
||||
}
|
||||
}
|
@ -1 +1 @@
|
||||
3.0.0-SNAPSHOT
|
||||
3.0.1-SNAPSHOT
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user