Merge pull request #7882 from swagger-api/issue-7839-null-fields

removed null fields
This commit is contained in:
HugoMario
2018-03-20 21:07:26 -05:00
committed by GitHub
@@ -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 class SwaggerYamlGenerator extends DefaultCodegen implements CodegenConfi
@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 class SwaggerYamlGenerator extends DefaultCodegen implements CodegenConfi
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);
}
}