Minor enhancements to the Avro schema generator (#3728)

* minor enhancements to the avro generator

* add option to customize package name

* update doc
This commit is contained in:
William Cheng 2019-08-22 16:06:04 +08:00 committed by GitHub
parent 870bb1c858
commit 8741a8ca35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 336 additions and 26 deletions

View File

@ -756,6 +756,7 @@ Here is a list of template creators:
* Configuration
* Apache2: @stkrwork
* Schema
* Avro: @sgadouar
* GraphQL: @wing328 [:heart:](https://www.patreon.com/wing328)
* MySQL: @ybelenko

2
bin/openapi3/avro-petstore.sh Normal file → Executable file
View File

@ -27,6 +27,6 @@ fi
# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g avro -o samples/openapi3/schema/petstore/avro $@"
ags="generate -t modules/openapi-generator/src/main/resources/avro-schema -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g avro-schema -o samples/openapi3/schema/petstore/avro-schema $@"
java $JAVA_OPTS -jar $executable $ags

View File

@ -125,6 +125,7 @@ The following generators are available:
## SCHEMA generators
* [avro-schema (beta)](generators/avro-schema)
* [mysql-schema](generators/mysql-schema)

View File

@ -0,0 +1,14 @@
---
id: generator-opts-schema-avro-schema
title: Config Options for avro-schema
sidebar_label: avro-schema
---
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|packageName|package for generated classes (where supported)| |null|

View File

@ -17,24 +17,36 @@
package org.openapitools.codegen.languages;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import static org.openapitools.codegen.utils.StringUtils.camelize;
public class AvroCodegen extends DefaultCodegen implements CodegenConfig {
private static final String AVRO = "avro";
public class AvroSchemaCodegen extends DefaultCodegen implements CodegenConfig {
private static final String AVRO = "avro-schema";
protected String packageName = "model";
public AvroCodegen() {
outputFolder = "generated-code/avro";
public AvroSchemaCodegen() {
super();
generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.BETA)
.build();
outputFolder = "generated-code/avro-schema";
modelTemplateFiles.put("model.mustache", ".avsc");
apiPackage = "api";
modelPackage = "model";
importMapping.clear();
embeddedTemplateDir = templateDir = AVRO;
// default HIDE_GENERATION_TIMESTAMP to true
hideGenerationTimestamp = Boolean.TRUE;
@ -61,18 +73,18 @@ public class AvroCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("UUID", "string");
typeMapping.put("BigDecimal", "string");
importMapping.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, CodegenConstants.PACKAGE_NAME_DESC));
}
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC).defaultValue("src"));
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.LICENSE_NAME, "name of the license the project uses (Default: using info.license.name)"));
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC).defaultValue(Boolean.TRUE.toString()));
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue("camelCase"));
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
packageName = (String) additionalProperties.get(CodegenConstants.PACKAGE_NAME);
}
additionalProperties.put("packageName", packageName);
embeddedTemplateDir = AVRO;
templateDir = AVRO;
}
@Override
@ -82,17 +94,17 @@ public class AvroCodegen extends DefaultCodegen implements CodegenConfig {
@Override
public String getName() {
return "avro";
return "avro-schema";
}
@Override
public String getHelp() {
return "Generates a Avro model.";
return "Generates a Avro model (beta).";
}
@Override
public String modelFileFolder() {
return outputFolder + "/" ;
return outputFolder + File.separator;
}
@Override
@ -104,8 +116,20 @@ public class AvroCodegen extends DefaultCodegen implements CodegenConfig {
protected void setNonArrayMapProperty(CodegenProperty property, String type) {
super.setNonArrayMapProperty(property, type);
if (property.isModel) {
property.dataType = camelize(modelNamePrefix + property.dataType + modelNameSuffix );
property.dataType = camelize(modelNamePrefix + property.dataType + modelNameSuffix);
}
}
@Override
public String escapeUnsafeCharacters(String input) {
// do nothing as it's a schema conversion
return input;
}
@Override
public String escapeQuotationMark(String input) {
// do nothing as it's a schema conversion
return input;
}
}

View File

@ -4,7 +4,7 @@ org.openapitools.codegen.languages.AndroidClientCodegen
org.openapitools.codegen.languages.Apache2ConfigCodegen
org.openapitools.codegen.languages.ApexClientCodegen
org.openapitools.codegen.languages.AspNetCoreServerCodegen
org.openapitools.codegen.languages.AvroCodegen
org.openapitools.codegen.languages.AvroSchemaCodegen
org.openapitools.codegen.languages.BashClientCodegen
org.openapitools.codegen.languages.CLibcurlClientCodegen
org.openapitools.codegen.languages.ClojureClientCodegen

View File

@ -1,8 +1,11 @@
{{#models}}{{#model}}{
"namespace": "{{package}}",
{{#models}}
{{#model}}
{
"namespace": "{{packageName}}",
"type": "{{#isEnum}}enum{{/isEnum}}{{^isEnum}}record{{/isEnum}}",
"doc": "{{{description}}}",
"name": "{{{classname}}}",
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>pojo}}{{/isEnum}}
}
{{/model}}{{/models}}
{{/model}}
{{/models}}

View File

@ -1,7 +1,9 @@
"fields": [{{#vars}}{{^-first}},{{/-first}}
"fields": [
{{#vars}}
{
"name": "{{baseName}}",
"type": {{^required}}["null", {{/required}}{{>typeProperty}}{{^required}}]{{/required}},
"doc": "{{{description}}}"
}{{/vars}}
}{{^-last}},{{/-last}}
{{/vars}}
]

View File

@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.
# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

View File

@ -0,0 +1 @@
4.1.1-SNAPSHOT

View File

@ -0,0 +1,23 @@
{
"namespace": "model",
"type": "record",
"doc": "Describes the result of uploading an image resource",
"name": "ApiResponse",
"fields": [
{
"name": "code",
"type": ["null", "int"],
"doc": ""
},
{
"name": "type",
"type": ["null", "string"],
"doc": ""
},
{
"name": "message",
"type": ["null", "string"],
"doc": ""
}
]
}

View File

@ -0,0 +1,18 @@
{
"namespace": "model",
"type": "record",
"doc": "A category for a pet",
"name": "Category",
"fields": [
{
"name": "id",
"type": ["null", "long"],
"doc": ""
},
{
"name": "name",
"type": ["null", "string"],
"doc": ""
}
]
}

View File

@ -0,0 +1,18 @@
{
"namespace": "model",
"type": "record",
"doc": "",
"name": "InlineObject",
"fields": [
{
"name": "name",
"type": ["null", "string"],
"doc": "Updated name of the pet"
},
{
"name": "status",
"type": ["null", "string"],
"doc": "Updated status of the pet"
}
]
}

View File

@ -0,0 +1,18 @@
{
"namespace": "model",
"type": "record",
"doc": "",
"name": "InlineObject1",
"fields": [
{
"name": "additionalMetadata",
"type": ["null", "string"],
"doc": "Additional data to pass to server"
},
{
"name": "file",
"type": ["null", "model.File"],
"doc": "file to upload"
}
]
}

View File

@ -0,0 +1,46 @@
{
"namespace": "model",
"type": "record",
"doc": "An order for a pets from the pet store",
"name": "Order",
"fields": [
{
"name": "id",
"type": ["null", "long"],
"doc": ""
},
{
"name": "petId",
"type": ["null", "long"],
"doc": ""
},
{
"name": "quantity",
"type": ["null", "int"],
"doc": ""
},
{
"name": "shipDate",
"type": ["null", "string"],
"doc": ""
},
{
"name": "status",
"type": ["null", {
"type": "enum",
"name": "Order_status",
"symbols": [
"placed",
"approved",
"delivered"
]
}],
"doc": "Order Status"
},
{
"name": "complete",
"type": ["null", "boolean"],
"doc": ""
}
]
}

View File

@ -0,0 +1,52 @@
{
"namespace": "model",
"type": "record",
"doc": "A pet for sale in the pet store",
"name": "Pet",
"fields": [
{
"name": "id",
"type": ["null", "long"],
"doc": ""
},
{
"name": "category",
"type": ["null", "model.Category"],
"doc": ""
},
{
"name": "name",
"type": "string",
"doc": ""
},
{
"name": "photoUrls",
"type": {
"type": "array",
"items": "string"
},
"doc": ""
},
{
"name": "tags",
"type": ["null", {
"type": "array",
"items": "model.Tag"
}],
"doc": ""
},
{
"name": "status",
"type": ["null", {
"type": "enum",
"name": "Pet_status",
"symbols": [
"available",
"pending",
"sold"
]
}],
"doc": "pet status in the store"
}
]
}

View File

@ -0,0 +1,18 @@
{
"namespace": "model",
"type": "record",
"doc": "A tag for a pet",
"name": "Tag",
"fields": [
{
"name": "id",
"type": ["null", "long"],
"doc": ""
},
{
"name": "name",
"type": ["null", "string"],
"doc": ""
}
]
}

View File

@ -0,0 +1,48 @@
{
"namespace": "model",
"type": "record",
"doc": "A User who is purchasing from the pet store",
"name": "User",
"fields": [
{
"name": "id",
"type": ["null", "long"],
"doc": ""
},
{
"name": "username",
"type": ["null", "string"],
"doc": ""
},
{
"name": "firstName",
"type": ["null", "string"],
"doc": ""
},
{
"name": "lastName",
"type": ["null", "string"],
"doc": ""
},
{
"name": "email",
"type": ["null", "string"],
"doc": ""
},
{
"name": "password",
"type": ["null", "string"],
"doc": ""
},
{
"name": "phone",
"type": ["null", "string"],
"doc": ""
},
{
"name": "userStatus",
"type": ["null", "int"],
"doc": "User Status"
}
]
}