Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMario authored Dec 27, 2019
2 parents 422780d + d466104 commit 7eb1e9c
Show file tree
Hide file tree
Showing 45 changed files with 227 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ private static String getTypeOfSchema(Schema schema) {
} else if (schema instanceof BinarySchema) {
return SchemaTypeUtil.BINARY_FORMAT;
} else if (schema instanceof FileSchema) {
return "file"; // FIXME: this type does not exist in the OpenAPI 3.0 specification
return SchemaTypeUtil.BINARY_FORMAT;
} else if (schema instanceof BooleanSchema) {
return SchemaTypeUtil.BOOLEAN_TYPE;
} else if (schema instanceof DateSchema) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package io.swagger.codegen.v3.generators.html;

import io.swagger.codegen.v3.CodegenModel;
import io.swagger.codegen.v3.CodegenModelFactory;
import io.swagger.codegen.v3.CodegenModelType;
import io.swagger.codegen.v3.CodegenProperty;
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
import io.swagger.codegen.v3.generators.SchemaHandler;
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class HtmlSchemaHandler extends SchemaHandler {

public HtmlSchemaHandler(DefaultCodegenConfig codegenConfig) {
super(codegenConfig);
}


public void createCodegenModel(ComposedSchema composedProperty, CodegenProperty codegenProperty) {
final List<Schema> oneOf = composedProperty.getOneOf();
final List<Schema> anyOf = composedProperty.getAnyOf();

if (oneOf != null && !oneOf.isEmpty()) {
if (!hasNonObjectSchema(oneOf)) {
final CodegenModel oneOfModel = createFromOneOfSchemas(oneOf);
codegenProperty.vendorExtensions.put("oneOf-model", oneOfModel);
}
}
if (anyOf != null && !anyOf.isEmpty()) {
if (!hasNonObjectSchema(anyOf)) {
final CodegenModel anyOfModel = createFromOneOfSchemas(anyOf);
codegenProperty.vendorExtensions.put("anyOf-model", anyOfModel);
}
}

}

public void configureComposedModelFromSchemaItems(CodegenModel codegenModel, ComposedSchema items) {
List<Schema> oneOfList = items.getOneOf();
if (oneOfList != null && !oneOfList.isEmpty()){
String name = "OneOf" + codegenModel.name + "Items";
final CodegenModel oneOfModel = createComposedModel(name);
// setting name to be used as instance type on composed model.
items.addExtension("x-model-name", codegenConfig.toModelName(name));

final List<String> modelNames = new ArrayList<>();
for (Schema interfaceSchema : oneOfList) {
if (StringUtils.isNotBlank(interfaceSchema.get$ref())) {
String schemaName = OpenAPIUtil.getSimpleRef(interfaceSchema.get$ref());
modelNames.add(codegenConfig.toModelName(schemaName));
}
}
oneOfModel.vendorExtensions.put("x-model-names", modelNames);
if (!modelNames.isEmpty()) {
codegenModel.vendorExtensions.put("oneOf-model", oneOfModel);
}
}
List<Schema> anyOfList = items.getAnyOf();
if (anyOfList != null && !anyOfList.isEmpty()){
String name = "AnyOf" + codegenModel.name + "Items";
final CodegenModel anyOfModel = createComposedModel(name);
items.addExtension("x-model-name", codegenConfig.toModelName(name));

final List<String> modelNames = new ArrayList<>();
for (Schema interfaceSchema : anyOfList) {
if (StringUtils.isNotBlank(interfaceSchema.get$ref())) {
String schemaName = OpenAPIUtil.getSimpleRef(interfaceSchema.get$ref());
modelNames.add(codegenConfig.toModelName(schemaName));
}
}
anyOfModel.vendorExtensions.put("x-model-names", modelNames);
if (!modelNames.isEmpty()) {
codegenModel.vendorExtensions.put("anyOf-model", anyOfModel);
}
}
}

public void configureOneOfModel(CodegenModel codegenModel, List<Schema> oneOf) {
// no ops for html generator
}

public void configureAnyOfModel(CodegenModel codegenModel, List<Schema> anyOf) {
// no ops for html generator
}

public void configureOneOfModelFromProperty(CodegenProperty codegenProperty, CodegenModel codegenModel) {
// no ops for html generator
}

public void configureAnyOfModelFromProperty(CodegenProperty codegenProperty, CodegenModel codegenModel) {
// no ops for html generator
}

private CodegenModel createFromOneOfSchemas(List<Schema> schemas) {
final CodegenModel codegenModel = CodegenModelFactory.newInstance(CodegenModelType.MODEL);
final List<String> modelNames = new ArrayList<>();

for (Schema interfaceSchema : schemas) {
if (StringUtils.isNotBlank(interfaceSchema.get$ref())) {
String schemaName = OpenAPIUtil.getSimpleRef(interfaceSchema.get$ref());
modelNames.add(codegenConfig.toModelName(schemaName));
}
}
codegenModel.vendorExtensions.put("x-model-names", modelNames);
return codegenModel;
}

private CodegenModel createComposedModel(String name) {
final CodegenModel composedModel = CodegenModelFactory.newInstance(CodegenModelType.MODEL);
this.configureModel(composedModel, name);
return composedModel;
}

private void configureModel(CodegenModel codegenModel, String name) {
codegenModel.name = name;
codegenModel.classname = codegenConfig.toModelName(name);
codegenModel.classVarName = codegenConfig.toVarName(name);
codegenModel.classFilename = codegenConfig.toModelFilename(name);
codegenModel.vendorExtensions.put("x-is-composed-model", Boolean.TRUE);
}

private boolean hasNonObjectSchema(List<Schema> schemas) {
for (Schema schema : schemas) {
if (!codegenConfig.isObjectSchema(schema)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class StaticHtmlCodegen extends DefaultCodegenConfig {

public StaticHtmlCodegen() {
super();
schemaHandler = new HtmlSchemaHandler(this);
outputFolder = "docs";

defaultIncludes = new HashSet<String>();
Expand Down Expand Up @@ -155,7 +156,7 @@ public String escapeUnsafeCharacters(String input) {

/**
* Convert Markdown text to HTML
*
*
* @param input
* text in Markdown; may be null.
* @return the text, converted to Markdown. For null input, "" is returned.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/Java/api_doc.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ try {
{{^parameters}}This endpoint does not need any parameter.{{/parameters}}{{#parameters}}{{#@last}}
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------{{/@last}}{{/parameters}}
{{#parameters}} **{{paramName}}** | {{#is this 'primitive-type'}}**{{dataType}}**{{/is}}{{#isNot this 'primitive-type'}}{{#is this 'file'}}**{{dataType}}**{{/is}}{{#isNot this 'file'}}[**{{dataType}}**]({{baseType}}.md){{/isNot}}{{/isNot}}| {{description}} |{{^required}} [optional]{{/required}}{{#defaultValue}} [default to {{defaultValue}}]{{/defaultValue}}{{#allowableValues}} [enum: {{#values}}{{{.}}}{{^@last}}, {{/@last}}{{/values}}]{{/allowableValues}}
{{#parameters}} **{{paramName}}** | {{#is this 'primitive-type'}}**{{dataType}}**{{/is}}{{#isNot this 'primitive-type'}}{{#isBinary}}**{{dataType}}**{{/isBinary}}{{^isBinary}}[**{{dataType}}**]({{baseType}}.md){{/isBinary}}{{/isNot}}| {{description}} |{{^required}} [optional]{{/required}}{{#defaultValue}} [default to {{defaultValue}}]{{/defaultValue}}{{#allowableValues}} [enum: {{#values}}{{{.}}}{{^@last}}, {{/@last}}{{/values}}]{{/allowableValues}}
{{/parameters}}

### Return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class {{classname}} {
{{#hasFormParams}}
{{#formParams}}
if ({{paramName}} != null)
{{localVariablePrefix}}formParams.add("{{baseName}}", {{#is this 'file'}}new FileSystemResource({{paramName}}){{/is}}{{#isNot this 'file'}}{{paramName}}{{/isNot}});
{{localVariablePrefix}}formParams.add("{{baseName}}", {{#is this 'binary'}}new FileSystemResource({{paramName}}){{/is}}{{#isNot this 'binary'}}{{paramName}}{{/isNot}});
{{/formParams}}
{{/hasFormParams}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{~#is this 'form-param'}}{{#isNot this 'file'}}{{#is ../this 'multipart'}}@retrofit.http.Part{{/is}}{{#isNot ../this 'multipart'}}@retrofit.http.Field{{/isNot}}("{{baseName}}") {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'file'}}{{#is ../this 'multipart'}}@retrofit.http.Part{{/is}}{{#isNot ../this 'multipart'}}@retrofit.http.Field{{/isNot}}("{{baseName}}") TypedFile {{paramName}}{{/is}}{{/is}}
{{~#is this 'form-param'}}{{#isNot this 'binary'}}{{#is ../this 'multipart'}}@retrofit.http.Part{{/is}}{{#isNot ../this 'multipart'}}@retrofit.http.Field{{/isNot}}("{{baseName}}") {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'binary'}}{{#is ../this 'multipart'}}@retrofit.http.Part{{/is}}{{#isNot ../this 'multipart'}}@retrofit.http.Field{{/isNot}}("{{baseName}}") TypedFile {{paramName}}{{/is}}{{/is}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'form-param'}}{{#isNot this 'file'}}{{#is this 'multipart'}}@retrofit2.http.Part{{/is}}{{#isNot this 'multipart'}}@retrofit2.http.Field{{/isNot}}("{{baseName}}") {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'file'}}{{#is this 'multipart'}}@retrofit2.http.Part{{/is}}{{#isNot this 'multipart'}}@retrofit2.http.Field{{/isNot}}{{#usePlayWS}} okhttp3.MultipartBody.Part {{/usePlayWS}}{{^usePlayWS}}("{{baseName}}\"; filename=\"{{baseName}}") RequestBody {{/usePlayWS}}{{paramName}}{{/is}}{{/is}}
{{#is this 'form-param'}}{{#isNot this 'binary'}}{{#is this 'multipart'}}@retrofit2.http.Part{{/is}}{{#isNot this 'multipart'}}@retrofit2.http.Field{{/isNot}}("{{baseName}}") {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'binary'}}{{#is this 'multipart'}}@retrofit2.http.Part{{/is}}{{#isNot this 'multipart'}}@retrofit2.http.Field{{/isNot}}{{#usePlayWS}} okhttp3.MultipartBody.Part {{/usePlayWS}}{{^usePlayWS}}("{{baseName}}\"; filename=\"{{baseName}}") RequestBody {{/usePlayWS}}{{paramName}}{{/is}}{{/is}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'form-param'}}{{#notFile}}{{{dataType}}} {{paramName}}{{/notFile}}{{#is this 'file'}}FormDataContentDisposition fileDetail{{/is}}{{/is}}
{{#is this 'form-param'}}{{#isNot this 'binary'}}{{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'binary'}}FormDataContentDisposition fileDetail{{/is}}{{/is}}
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/JavaJaxRS/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class {{classname}} {
{{/useOas2}}
public Response {{nickname}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}},{{/parameters}}@Context SecurityContext securityContext)
throws NotFoundException {
return delegate.{{nickname}}({{#parameters}}{{#isFile}}{{paramName}}InputStream, {{paramName}}Detail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}},{{/parameters}}securityContext);
return delegate.{{nickname}}({{#parameters}}{{#isBinary}}{{paramName}}InputStream, {{paramName}}Detail{{/isBinary}}{{^isBinary}}{{paramName}}{{/isBinary}},{{/parameters}}securityContext);
}
{{/contents}}
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class {{classname}} {
@ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{^vendorExtensions.x-java-is-response-void}}, content = @Content({{^containerType}}schema = @Schema(implementation = {{{baseType}}}.class)){{/containerType}}{{#containerType}}array = @ArraySchema(schema = @Schema(implementation = {{{baseType}}}.class))){{/containerType}}{{/vendorExtensions.x-java-is-response-void}}){{#hasMore}},{{/hasMore}}{{/responses}} })
{{/useOas2}}
public Response {{nickname}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/parameters}}) {
return delegate.{{nickname}}({{#parameters}}{{#isFile}}{{paramName}}InputStream, {{paramName}}Detail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}}, {{/parameters}}securityContext);
return delegate.{{nickname}}({{#parameters}}{{#isBinary}}{{paramName}}InputStream, {{paramName}}Detail{{/isBinary}}{{^isBinary}}{{paramName}}{{/isBinary}}, {{/parameters}}securityContext);
}
{{/contents}}
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'form-param'}}{{#isNot this 'file'}}@Multipart(value = "{{baseName}}"{{^required}}, required = false{{/required}}) {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'file'}} @Multipart(value = "{{baseName}}"{{^required}}, required = false{{/required}}) InputStream {{paramName}}InputStream, @Multipart(value = "{{baseName}}" {{^required}}, required = false{{/required}}) Attachment {{paramName}}Detail{{/is}}{{/is}}
{{#is this 'form-param'}}{{#isNot this 'binary'}}@Multipart(value = "{{baseName}}"{{^required}}, required = false{{/required}}) {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'binary'}} @Multipart(value = "{{baseName}}"{{^required}}, required = false{{/required}}) InputStream {{paramName}}InputStream, @Multipart(value = "{{baseName}}" {{^required}}, required = false{{/required}}) Attachment {{paramName}}Detail{{/is}}{{/is}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'form-param'}}{{#isNot this 'file'}}{{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'file'}}InputStream {{paramName}}InputStream, Attachment {{paramName}}Detail{{/is}}{{/is}}
{{#is this 'form-param'}}{{#isNot this 'binary'}}{{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'binary'}}InputStream {{paramName}}InputStream, Attachment {{paramName}}Detail{{/is}}{{/is}}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class {{classname}}Test {
@Test
public void {{operationId}}Test() {
{{#parameters}}
{{^isFile}}{{{dataType}}} {{paramName}} = null;{{/isFile}}{{#isFile}}org.apache.cxf.jaxrs.ext.multipart.Attachment {{paramName}} = null;{{/isFile}}
{{^isBinary}}{{{dataType}}} {{paramName}} = null;{{/isBinary}}{{#isBinary}}org.apache.cxf.jaxrs.ext.multipart.Attachment {{paramName}} = null;{{/isBinary}}
{{/parameters}}
//{{^vendorExtensions.x-java-is-response-void}}{{>returnTypes}} response = {{/vendorExtensions.x-java-is-response-void}}api.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}});
{{^vendorExtensions.x-java-is-response-void}}//assertNotNull(response);{{/vendorExtensions.x-java-is-response-void}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'form-param'}}{{#isNot this 'file'}}@Multipart(value = "{{baseName}}"{{^required}}, required = false{{/required}}) {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'file'}} @Multipart(value = "{{baseName}}" {{^required}}, required = false{{/required}}) Attachment {{paramName}}Detail{{/is}}{{/is}}
{{#is this 'form-param'}}{{#isNot this 'binary'}}@Multipart(value = "{{baseName}}"{{^required}}, required = false{{/required}}) {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'binary'}} @Multipart(value = "{{baseName}}" {{^required}}, required = false{{/required}}) Attachment {{paramName}}Detail{{/is}}{{/is}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'form-param'}}{{#isNot this 'file'}}{{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'file'}} Attachment {{paramName}}Detail{{/is}}{{/is}}
{{#is this 'form-param'}}{{#isNot this 'binary'}}{{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'binary'}} Attachment {{paramName}}Detail{{/is}}{{/is}}
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/JavaJaxRS/di/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class {{classname}} {
{{/useOas2}}
public Response {{nickname}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}},{{/parameters}}@Context SecurityContext securityContext)
throws NotFoundException {
return delegate.{{nickname}}({{#parameters}}{{#isFile}}{{paramName}}InputStream, {{paramName}}Detail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}},{{/parameters}}securityContext);
return delegate.{{nickname}}({{#parameters}}{{#isBinary}}{{paramName}}InputStream, {{paramName}}Detail{{/isBinary}}{{^isBinary}}{{paramName}}{{/isBinary}},{{/parameters}}securityContext);
}
{{/contents}}
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{#is this 'form-param'}}{{#isNot this 'file'}}{{#useOas2}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, {{> allowableValues }}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{#vendorExtensions.x-multipart}}@FormDataParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{^vendorExtensions.x-multipart}} {{#defaultValue}} @DefaultValue("{{{defaultValue}}}"){{/defaultValue}} @FormParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{/useOas2}}{{^useOas2}}@Parameter(description = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, {{> allowableValues }}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{#vendorExtensions.x-multipart}}@FormDataParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{^vendorExtensions.x-multipart}} {{#defaultValue}} @DefaultValue("{{{defaultValue}}}"){{/defaultValue}} @FormParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{/useOas2}}{{/isNot}}{{#is this 'file'}}@FormDataParam("{{baseName}}") InputStream {{paramName}}InputStream,
{{#is this 'form-param'}}{{#isNot this 'binary'}}{{#useOas2}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, {{> allowableValues }}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{#vendorExtensions.x-multipart}}@FormDataParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{^vendorExtensions.x-multipart}} {{#defaultValue}} @DefaultValue("{{{defaultValue}}}"){{/defaultValue}} @FormParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{/useOas2}}{{^useOas2}}@Parameter(description = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, {{> allowableValues }}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{#vendorExtensions.x-multipart}}@FormDataParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{^vendorExtensions.x-multipart}} {{#defaultValue}} @DefaultValue("{{{defaultValue}}}"){{/defaultValue}} @FormParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{/useOas2}}{{/isNot}}{{#is this 'binary'}}@FormDataParam("{{baseName}}") InputStream {{paramName}}InputStream,
@FormDataParam("{{baseName}}") FormDataContentDisposition {{paramName}}Detail{{/is}}{{/is}}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class {{classname}} {
{{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}},
{{/parameters}}@Context SecurityContext securityContext)
throws NotFoundException {
return delegate.{{nickname}}({{#parameters}}{{#isFile}} {{paramName}}InputStream, {{paramName}}Detail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}},{{/parameters}}securityContext);
return delegate.{{nickname}}({{#parameters}}{{#isBinary}} {{paramName}}InputStream, {{paramName}}Detail{{/isBinary}}{{^isBinary}}{{paramName}}{{/isBinary}},{{/parameters}}securityContext);
}
{{/contents}}
{{/operation}}
Expand Down
Loading

0 comments on commit 7eb1e9c

Please sign in to comment.