add helper function to generate openapi yaml/json file

This commit is contained in:
wing328
2018-03-31 17:11:54 +08:00
parent e3a4bd6e68
commit eaed75229a
20 changed files with 55 additions and 69 deletions

View File

@@ -9,11 +9,13 @@ import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.samskivert.mustache.Mustache.Compiler;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.headers.Header;
@@ -4138,4 +4140,36 @@ public class DefaultCodegen implements CodegenConfig {
cliOptions.add(option);
}
/**
* generates OpenAPI specification file in JSON format
*
* @param objs map of object
*/
public void generateJSONSpecFile(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI) objs.get("openapi");
if (openAPI != null) {
try {
objs.put("openapi-json", Json.pretty().writeValueAsString(openAPI).replace("\r\n", "\n"));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
/**
* generates OpenAPI specification file in YAML format
*
* @param objs map of object
*/
public void generateYAMLSpecFile(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI) objs.get("openapi");
if (openAPI != null) {
try {
objs.put("openapi-yaml", Yaml.mapper().writeValueAsString(openAPI));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
}

View File

@@ -372,14 +372,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI) objs.get("openapi");
if (openAPI != null) {
try {
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(openAPI));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
generateYAMLSpecFile(objs);
return super.postProcessSupportingFileData(objs);
}

View File

@@ -521,14 +521,7 @@ public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenC
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI) objs.get("openapi");
if (openAPI != null) {
try {
objs.put("openapi-yaml", Yaml.mapper().writeValueAsString(openAPI));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
generateJSONSpecFile(objs);
return super.postProcessSupportingFileData(objs);
}

View File

@@ -415,22 +415,8 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI)objs.get("openapi");
if (openAPI != null) {
try {
SimpleModule module = new SimpleModule();
module.addSerializer(Double.class, new JsonSerializer<Double>() {
@Override
public void serialize(Double val, JsonGenerator jgen,
SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeNumber(new BigDecimal(val));
}
});
objs.put("swagger-yaml", Yaml.mapper().registerModule(module).writeValueAsString(openAPI));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
generateYAMLSpecFile(objs);
for (Map<String, Object> operations : getOperations(objs)) {
@SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");

View File

@@ -365,14 +365,8 @@ public class PythonFlaskConnexionServerCodegen extends DefaultCodegen implements
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
OpenAPI swagger = (OpenAPI) objs.get("openapi");
if (swagger != null) {
try {
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(swagger));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
generateYAMLSpecFile(objs);
for (Map<String, Object> operations : getOperations(objs)) {
@SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");

View File

@@ -315,14 +315,7 @@ public class RubyOnRailsServerCodegen extends DefaultCodegen implements CodegenC
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI) objs.get("swagger");
if(openAPI != null) {
try {
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(openAPI));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
generateYAMLSpecFile(objs);
return super.postProcessSupportingFileData(objs);
}

View File

@@ -240,14 +240,7 @@ public class RubySinatraServerCodegen extends DefaultCodegen implements CodegenC
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI) objs.get("openapi");
if (openAPI != null) {
try {
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(openAPI));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
generateYAMLSpecFile(objs);
return super.postProcessSupportingFileData(objs);
}

View File

@@ -1 +1 @@
{{{swagger-json}}}
{{{openapi-json}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-json}}}
{{{openapi-json}}}

View File

@@ -1 +1 @@
{{{swagger-json}}}
{{{openapi-json}}}

View File

@@ -1 +1 @@
{{{swagger-json}}}
{{{openapi-json}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-yaml}}}
{{{openapi-yaml}}}

View File

@@ -1 +1 @@
{{{swagger-json}}}
{{{openapi-json}}}