Skip to content

Commit

Permalink
Merge pull request #7882 from swagger-api/issue-7839-null-fields
Browse files Browse the repository at this point in the history
removed null fields
  • Loading branch information
HugoMario authored Mar 21, 2018
2 parents fedfb0c + 74fa3b1 commit 22f0b8d
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

0 comments on commit 22f0b8d

Please # to comment.