Skip to content

Commit

Permalink
Fixing issue #11 for 1.x.x branch. (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: kaisa <[email protected]>
  • Loading branch information
KatseNarniaan and kaisa authored Apr 5, 2021
1 parent ff86799 commit fb30405
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
.idea
*.iml
.settings
33 changes: 18 additions & 15 deletions src/main/java/io/openapitools/swagger/GenerateMojo.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package io.openapitools.swagger;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.Set;

import io.openapitools.swagger.config.SwaggerConfig;
import io.swagger.jaxrs.Reader;
import io.swagger.models.Swagger;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.Set;

/**
* Maven mojo to generate OpenAPI documentation document based on Swagger.
*/
Expand Down Expand Up @@ -68,6 +64,13 @@ public class GenerateMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;

/**
* When true, the plugin produces a pretty-printed JSON Swagger specification. Note that this parameter doesn't
* have any effect on the generation of the YAML version because YAML is pretty-printed by nature.
*/
@Parameter(defaultValue = "false")
private boolean prettyPrint;

@Component
private MavenProjectHelper projectHelper;

Expand All @@ -91,7 +94,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
outputFormats.forEach(format -> {
try {
File outputFile = new File(outputDirectory, outputFilename + "." + format.name().toLowerCase());
format.write(swagger, outputFile);
format.write(swagger, outputFile, prettyPrint);
if (attachSwaggerArtifact) {
projectHelper.attachArtifact(project, format.name().toLowerCase(), "swagger", outputFile);
}
Expand All @@ -104,7 +107,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
private URLClassLoader createClassLoader() {
try {
File compiled = new File(project.getBuild().getOutputDirectory());
return new URLClassLoader(new URL[] {compiled.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
return new URLClassLoader(new URL[]{compiled.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
} catch (MalformedURLException e) {
throw new RuntimeException("Unable to create class loader with compiled classes", e);
}
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/io/openapitools/swagger/OutputFormat.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.openapitools.swagger;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.swagger.models.Swagger;
import io.swagger.util.Json;
import io.swagger.util.Yaml;

import java.io.File;
import java.io.IOException;

/**
* Supported output formats.
*/
Expand All @@ -22,16 +23,16 @@ public enum OutputFormat {
this.writer = writer;
}

public void write(Swagger swagger, File file) throws IOException {
writer.write(swagger, file);
public void write(Swagger swagger, File file, boolean prettyPrint) throws IOException {
writer.write(swagger, file, prettyPrint);
}

/**
* Interface defining requirements for being able to write out Swagger instance to file.
*/
@FunctionalInterface
interface SwaggerWriter {
void write(Swagger swagger, File file) throws IOException;
void write(Swagger swagger, File file, boolean prettyPrint) throws IOException;
}

/**
Expand All @@ -40,9 +41,11 @@ interface SwaggerWriter {
static class JSONWriter implements SwaggerWriter {

@Override
public void write(Swagger swagger, File file) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
public void write(Swagger swagger, File file, boolean prettyPrint) throws IOException {
ObjectMapper mapper = Json.mapper();
if (prettyPrint) {
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
mapper.writeValue(file, swagger);
}
}
Expand All @@ -53,7 +56,7 @@ public void write(Swagger swagger, File file) throws IOException {
static class YAMLWriter implements SwaggerWriter {

@Override
public void write(Swagger swagger, File file) throws IOException {
public void write(Swagger swagger, File file, boolean prettyPrint) throws IOException {
Yaml.mapper().writeValue(file, swagger);
}
}
Expand Down

0 comments on commit fb30405

Please sign in to comment.