Skip to content
Merged
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
1 change: 1 addition & 0 deletions codegen-cli/src/main/resources/config/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ singletons:
- com.networknt.codegen.graphql.GraphqlGenerator
- com.networknt.codegen.eventuate.EventuateOpenApiGenerator
- com.networknt.codegen.rest.OpenApiKotlinGenerator
- com.networknt.codegen.rest.OpenApiSpecGenerator
10 changes: 9 additions & 1 deletion light-rest-4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package com.networknt.codegen.rest;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsoniter.any.Any;
import com.networknt.codegen.Generator;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;

public class OpenApiSpecGenerator implements Generator {
private static final Logger logger = LoggerFactory.getLogger(OpenApiSpecGenerator.class);

private static final String FRAMEWORK="openapi-spec";

/** -- configuration items begin -- */
private static final String CONFIG_SPECGENERATION ="specGeneration";
// comma delimited package names
private static final String CONFIG_MODELPACKAGES ="modelPackages";
// absolute path of an existing spec file. If this is specified, the generated models (a.k.a. components) will be added to this file and override existing schemas, if any.
private static final String CONFIG_MERGETO ="mergeTo";
// comma delimited formats, currently support json, yml, or yaml
private static final String CONFIG_OUTPUTFORMAT="outputFormat";
// the output file name without extension
private static final String CONFIG_OUTPUTFILENAME="outputFilename";
/** -- configuration items end -- */

private static final String DOT = ".";
private static final String COMMA_SPACE = "\\s*,\\s*";
private static final String JSON="json";
private static final String YAML="yaml";
private static final String YML="yml";
private static final String DEFAULT_OUTPUT_NAME="openapi_generated";


@Override
public String getFramework() {
return FRAMEWORK;
}

@SuppressWarnings("rawtypes")
@Override
public void generate(String targetPath, Object model, Any config) throws IOException {
if (StringUtils.isBlank(targetPath)) {
logger.error("Output location is not specified.");
return;
}

if (!config.keys().contains(CONFIG_SPECGENERATION)) {
logger.error("Missing config: cannot find {} in the specified config file", CONFIG_SPECGENERATION);
return;
}

Map<String, Any> genConfig = config.get(CONFIG_SPECGENERATION).asMap();
String modelPackages = StringUtils.trimToEmpty(genConfig.get(CONFIG_MODELPACKAGES).toString());
String mergeTo = StringUtils.trimToEmpty(genConfig.get(CONFIG_MERGETO).toString());
String outputFormat = StringUtils.trimToEmpty(genConfig.get(CONFIG_OUTPUTFORMAT).toString());
String outputFilename = StringUtils.trimToEmpty(genConfig.get(CONFIG_OUTPUTFILENAME).toString());

File output_dir = new File(targetPath);

if (!output_dir.exists() || !output_dir.isDirectory()) {
output_dir.mkdirs();
}

String[] basePackageArray = modelPackages.split(COMMA_SPACE);

Map<String, Schema> schemas = new HashMap<>();

for (String packageName: basePackageArray) {
try (ScanResult scanResult =
new ClassGraph()
.enableClassInfo()
.whitelistPackages(packageName)
.scan()) {

List<Class<?>> classes = scanResult.getAllClasses().loadClasses();

for (Class<?> cls: classes) {
schemas.putAll(ModelConverters.getInstance().read(cls));
}
}
}

OpenAPI openApi = new OpenAPI();

openApi.setComponents(new Components().schemas(schemas));

openApi = merge(openApi, mergeTo);

String[] formats = outputFormat.split(COMMA_SPACE);

String filename = StringUtils.isBlank(outputFilename)?DEFAULT_OUTPUT_NAME:outputFilename;

for (String format: formats) {
dump(openApi, format, new File(output_dir, filename + DOT + format));
}
}

private void dump(OpenAPI openApi, String format, File outputFile) throws IOException {
String specStr=StringUtils.EMPTY;

if (StringUtils.equalsIgnoreCase(format, JSON)) {
specStr = Json.pretty(openApi);
}else if (StringUtils.equalsIgnoreCase(format, YML) || StringUtils.equalsIgnoreCase(format, YAML)){
specStr = Yaml.pretty(openApi);
}else {
throw new UnsupportedOperationException("Unknow output format " + format);
}

Files.write(Paths.get(outputFile.toURI()), specStr.getBytes());
}

@SuppressWarnings("rawtypes")
private OpenAPI merge(OpenAPI generatedSpec, String mergeTo) {
if (StringUtils.isNotBlank(mergeTo)) {
File destFile = new File(mergeTo);

if (destFile.isFile()) {
try {
OpenAPI openAPI=null;
String ext = getFileExtension(destFile);
if (StringUtils.equalsIgnoreCase(ext, JSON)) {
openAPI = Json.mapper().readValue(destFile, OpenAPI.class);
}else if (StringUtils.equalsIgnoreCase(ext, YML)||StringUtils.equalsIgnoreCase(ext, YAML)) {
openAPI = Yaml.mapper().readValue(destFile, OpenAPI.class);
}else {
throw new UnsupportedOperationException("Unknow file format " + ext);
}

if (null!=openAPI) {
Components components = openAPI.getComponents();

if (null == components) {
components = new Components();
}

Map<String, Schema> schemas = new HashMap<>();

schemas.putAll(components.getSchemas());

schemas.putAll(generatedSpec.getComponents().getSchemas());

components.setSchemas(schemas);

openAPI.setComponents(components);

return openAPI;
}

} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}

return generatedSpec;
}

private String getFileExtension(File file) {
String name = file.getName();
int lastIndexOf = name.lastIndexOf(DOT);
if (lastIndexOf < 0) {
return StringUtils.EMPTY;
}
return name.substring(lastIndexOf+1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.networknt.codegen.rest;

import java.io.IOException;

import org.junit.Ignore;
import org.junit.Test;

import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.networknt.codegen.OpenApiGeneratorTest;

@Ignore
public class OpenApiSpecGeneratorTest {
private static final String configName = "/config.json";
private static final String outputDir = "/tmp/codegen/";

@Test
public void test() throws IOException {
Any anyConfig = JsonIterator.parse(OpenApiGeneratorTest.class.getResourceAsStream(configName), 1024).readAny();

OpenApiSpecGenerator generator = new OpenApiSpecGenerator();

generator.generate(outputDir, null, anyConfig);
}
}
6 changes: 6 additions & 0 deletions light-rest-4j/src/test/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@
"handerl.yml",
"values.yml"
]
},
"specGeneration": {
"modelPackages": "com.networknt.petstore.model",
"mergeTo": "/tmp/codegen/openapi.json",
"outputFormat": "yaml, json",
"outputFilename": "openapi_gen_test"
}
}
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,18 @@
<version.zookeeper>3.5.3-beta</version.zookeeper>
<version.zkclient>0.3</version.zkclient>
<version.curator>4.0.0</version.curator>
<version.snakeyaml>1.20</version.snakeyaml>
<version.snakeyaml>1.24</version.snakeyaml>
<version.rocker>1.2.1</version.rocker>
<version.jcommander>1.72</version.jcommander>
<version.fastscanner>2.18.1</version.fastscanner>
<version.graphql>8.0</version.graphql>
<version.qdox>2.0-M9</version.qdox>
<versions.maven-version>2.4</versions.maven-version>
<versions.classgraph>4.8.22</versions.classgraph>
<version.swagger>2.0.7</version.swagger>
<argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
</properties>

<modules>
<module>codegen-core</module>
<module>light-rest-4j</module>
Expand Down Expand Up @@ -297,6 +299,16 @@
<artifactId>qdox</artifactId>
<version>${version.qdox}</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>${versions.classgraph}</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
<version>${version.swagger}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down