From e3bbec49605993e9009f57475ae1c6eb8b60f492 Mon Sep 17 00:00:00 2001 From: frantuma Date: Fri, 27 Dec 2019 17:46:52 +0100 Subject: [PATCH] fix html generator for composed schema properties --- .../v3/generators/html/HtmlSchemaHandler.java | 135 ++++++++++++++++++ .../v3/generators/html/StaticHtmlCodegen.java | 3 +- .../handlebars/htmlDocs/index.mustache | 30 ++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/swagger/codegen/v3/generators/html/HtmlSchemaHandler.java diff --git a/src/main/java/io/swagger/codegen/v3/generators/html/HtmlSchemaHandler.java b/src/main/java/io/swagger/codegen/v3/generators/html/HtmlSchemaHandler.java new file mode 100644 index 0000000000..8748a79208 --- /dev/null +++ b/src/main/java/io/swagger/codegen/v3/generators/html/HtmlSchemaHandler.java @@ -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 oneOf = composedProperty.getOneOf(); + final List 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 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 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 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 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 oneOf) { + // no ops for html generator + } + + public void configureAnyOfModel(CodegenModel codegenModel, List 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 schemas) { + final CodegenModel codegenModel = CodegenModelFactory.newInstance(CodegenModelType.MODEL); + final List 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 schemas) { + for (Schema schema : schemas) { + if (!codegenConfig.isObjectSchema(schema)) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/io/swagger/codegen/v3/generators/html/StaticHtmlCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/html/StaticHtmlCodegen.java index 2a50d45985..d6f8ee140b 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/html/StaticHtmlCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/html/StaticHtmlCodegen.java @@ -32,6 +32,7 @@ public class StaticHtmlCodegen extends DefaultCodegenConfig { public StaticHtmlCodegen() { super(); + schemaHandler = new HtmlSchemaHandler(this); outputFolder = "docs"; defaultIncludes = new HashSet(); @@ -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. diff --git a/src/main/resources/handlebars/htmlDocs/index.mustache b/src/main/resources/handlebars/htmlDocs/index.mustache index 3130275287..59b27c891a 100644 --- a/src/main/resources/handlebars/htmlDocs/index.mustache +++ b/src/main/resources/handlebars/htmlDocs/index.mustache @@ -174,6 +174,36 @@ {{#example}}
example: {{example}}
{{/example}} + {{#vendorExtensions.oneOf-model}} +
oneOf: + {{#vendorExtensions.x-model-names}} + {{this}}    + {{/vendorExtensions.x-model-names}} +
+ {{/vendorExtensions.oneOf-model}} + {{#vendorExtensions.anyOf-model}} +
anyOf: + {{#vendorExtensions.x-model-names}} + {{this}}    + {{/vendorExtensions.x-model-names}} +
+ {{/vendorExtensions.anyOf-model}} + {{#items}} + {{#vendorExtensions.oneOf-model}} +
items oneOf: + {{#vendorExtensions.x-model-names}} + {{this}}    + {{/vendorExtensions.x-model-names}} +
+ {{/vendorExtensions.oneOf-model}} + {{#vendorExtensions.anyOf-model}} +
items anyOf: + {{#vendorExtensions.x-model-names}} + {{this}}    + {{/vendorExtensions.x-model-names}} +
+ {{/vendorExtensions.anyOf-model}} + {{/items}} {{/vars}}