From 74fa3b1e13e86e57fa34883dc23fff7649741f02 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Tue, 20 Mar 2018 18:07:26 -0500 Subject: [PATCH] removed null fields --- .../languages/SwaggerYamlGenerator.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwaggerYamlGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwaggerYamlGenerator.java index bfea117164c8..b07d88fe5f0c 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwaggerYamlGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwaggerYamlGenerator.java @@ -1,11 +1,15 @@ package io.swagger.codegen.languages; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.Module; 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.swagger.codegen.*; import io.swagger.models.Swagger; -import io.swagger.util.Yaml; +import io.swagger.util.DeserializationModule; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,8 +60,8 @@ public void processOpts() { @Override public void processSwagger(Swagger swagger) { try { - final ObjectMapper mapper = new ObjectMapper(new YAMLFactory() - .configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)); + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)); + configureMapper(mapper); String swaggerString = mapper.writeValueAsString(swagger); String outputFile = outputFolder + File.separator + this.outputFile; FileUtils.writeStringToFile(new File(outputFile), swaggerString); @@ -79,4 +83,12 @@ public String escapeUnsafeCharacters(String input) { return input; } + private void configureMapper(ObjectMapper mapper) { + Module deserializerModule = new DeserializationModule(true, true); + mapper.registerModule(deserializerModule); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } }