Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ It possible to customise the following plugin properties:
* The default value is: openapi.json
* skip: Skip execution if set to true.
* The default value is: false
* format: Formats the json with indentation.
* The default value is: false
* Should be used with JSON only.
* headers: List of headers to send in request
* The default value is empty

Expand All @@ -89,6 +92,7 @@ It possible to customise the following plugin properties:
<outputFileName>openapi.json</outputFileName>
<outputDir>/home/springdoc/maven-output</outputDir>
<skip>false</skip>
<format>true</format>
<headers>
<header1key>header1value</header1key>
<header2key>header2value</header2key>
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
<version>3.6.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency>

</dependencies>

<build>
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
* Generate a openapi specification file.
Expand Down Expand Up @@ -80,6 +82,12 @@ public class SpringDocMojo extends AbstractMojo {
@Parameter(defaultValue = "false", property = "springdoc.skip")
private boolean skip;

/**
* Formats the openapi.json file if set to true. Default is false.
*/
@Parameter(defaultValue = "false", property = "springdoc.format")
private boolean format;

/**
* Headers to send in request
*/
Expand Down Expand Up @@ -110,6 +118,12 @@ public void execute() {
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String result = this.readFullyAsString(connection.getInputStream());

if (format && outputFileName.endsWith(DEFAULT_OUTPUT_EXTENSION)) {
JSONObject object = new JSONObject(new JSONTokener(result));
result = object.toString(2);
}

outputDir.mkdirs();
Files.write(Paths.get(outputDir.getAbsolutePath() + "/" + outputFileName), result.getBytes(StandardCharsets.UTF_8));
if (attachArtifact) addArtifactToMaven();
Expand All @@ -123,7 +137,7 @@ public void execute() {


/**
* Read fully as string string.
* Read fully as string.
*
* @param inputStream the input stream
* @return the string
Expand Down