Skip to content

Commit

Permalink
Merge pull request #7864 from swagger-api/issue-7839
Browse files Browse the repository at this point in the history
added config to yaml mapper to minimize long text.
  • Loading branch information
HugoMario authored Mar 18, 2018
2 parents e22faf4 + 96a9ba1 commit 9d85c82
Show file tree
Hide file tree
Showing 3 changed files with 861 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.swagger.codegen.languages;

import com.fasterxml.jackson.databind.ObjectMapper;
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;
Expand Down Expand Up @@ -53,7 +56,9 @@ public void processOpts() {
@Override
public void processSwagger(Swagger swagger) {
try {
String swaggerString = Yaml.mapper().writeValueAsString(swagger);
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()
.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
String swaggerString = mapper.writeValueAsString(swagger);
String outputFile = outputFolder + File.separator + this.outputFile;
FileUtils.writeStringToFile(new File(outputFile), swaggerString);
LOGGER.debug("wrote file to " + outputFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.swagger.codegen.swaggeryaml;

import io.swagger.codegen.ClientOptInput;
import io.swagger.codegen.DefaultGenerator;
import io.swagger.codegen.config.CodegenConfigurator;
import org.apache.commons.io.FileUtils;
import org.junit.rules.TemporaryFolder;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;

public class SwaggerYamlGeneratorTest {

@Test
public void testLongText() throws Exception {
final String LONG_DESCRIPTION_SAMPLE = "Are creatures of the cosmos Rig Veda? Trillion! Made in the interiors of collapsing stars Apollonius of Perga, globular star cluster emerged into consciousness bits of moving fluff brain is the seed of intelligence citizens of distant epochs another world courage of our questions a mote of dust suspended in a sunbeam ship of the imagination, paroxysm of global death intelligent beings? Two ghostly white figures in coveralls and helmets are soflty dancing hearts of the stars brain is the seed of intelligence quasar, Drake Equation billions upon billions and billions upon billions upon billions upon billions upon billions upon billions upon billions";
final String LONG_DESCRIPTION_SAMPLE2 = "Light years, culture, dispassionate extraterrestrial observer citizens of distant epochs intelligent beings Jean-Francois Champollion encyclopaedia galactica Sea of Tranquility emerged into consciousness Cambrian explosion another world Cambrian explosion globular star cluster. Emerged into consciousness take root and flourish explorations a mote of dust suspended in a sunbeam encyclopaedia galactica, not a sunrise but a galaxyrise, Orion's sword encyclopaedia galactica vastness is bearable only through love cosmos. Birth and billions upon billions upon billions upon billions upon billions upon billions upon billions.";

final TemporaryFolder folder = new TemporaryFolder();
folder.create();
final File output = folder.getRoot();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setLang("swagger-yaml")
.setInputSpec("src/test/resources/2_0/long_description_issue_7839.json")
.setOutputDir(output.getAbsolutePath());

final ClientOptInput clientOptInput = configurator.toClientOptInput();
new DefaultGenerator().opts(clientOptInput).generate();

File outputFile = new File(output, "swagger.yaml");
Assert.assertTrue(outputFile.exists());

String content = FileUtils.readFileToString(outputFile);

Assert.assertTrue(content.contains(LONG_DESCRIPTION_SAMPLE));
Assert.assertTrue(content.contains(LONG_DESCRIPTION_SAMPLE2));

folder.delete();

}
}
Loading

0 comments on commit 9d85c82

Please sign in to comment.