|
| 1 | +package io.swagger.codegen.v3.generators.html; |
| 2 | + |
| 3 | +import io.swagger.codegen.v3.CodegenModel; |
| 4 | +import io.swagger.codegen.v3.CodegenModelFactory; |
| 5 | +import io.swagger.codegen.v3.CodegenModelType; |
| 6 | +import io.swagger.codegen.v3.CodegenProperty; |
| 7 | +import io.swagger.codegen.v3.generators.DefaultCodegenConfig; |
| 8 | +import io.swagger.codegen.v3.generators.SchemaHandler; |
| 9 | +import io.swagger.codegen.v3.generators.util.OpenAPIUtil; |
| 10 | +import io.swagger.v3.oas.models.media.ComposedSchema; |
| 11 | +import io.swagger.v3.oas.models.media.Schema; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class HtmlSchemaHandler extends SchemaHandler { |
| 18 | + |
| 19 | + public HtmlSchemaHandler(DefaultCodegenConfig codegenConfig) { |
| 20 | + super(codegenConfig); |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + public void createCodegenModel(ComposedSchema composedProperty, CodegenProperty codegenProperty) { |
| 25 | + final List<Schema> oneOf = composedProperty.getOneOf(); |
| 26 | + final List<Schema> anyOf = composedProperty.getAnyOf(); |
| 27 | + |
| 28 | + if (oneOf != null && !oneOf.isEmpty()) { |
| 29 | + if (!hasNonObjectSchema(oneOf)) { |
| 30 | + final CodegenModel oneOfModel = createFromOneOfSchemas(oneOf); |
| 31 | + codegenProperty.vendorExtensions.put("oneOf-model", oneOfModel); |
| 32 | + } |
| 33 | + } |
| 34 | + if (anyOf != null && !anyOf.isEmpty()) { |
| 35 | + if (!hasNonObjectSchema(anyOf)) { |
| 36 | + final CodegenModel anyOfModel = createFromOneOfSchemas(anyOf); |
| 37 | + codegenProperty.vendorExtensions.put("anyOf-model", anyOfModel); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + public void configureComposedModelFromSchemaItems(CodegenModel codegenModel, ComposedSchema items) { |
| 44 | + List<Schema> oneOfList = items.getOneOf(); |
| 45 | + if (oneOfList != null && !oneOfList.isEmpty()){ |
| 46 | + String name = "OneOf" + codegenModel.name + "Items"; |
| 47 | + final CodegenModel oneOfModel = createComposedModel(name); |
| 48 | + // setting name to be used as instance type on composed model. |
| 49 | + items.addExtension("x-model-name", codegenConfig.toModelName(name)); |
| 50 | + |
| 51 | + final List<String> modelNames = new ArrayList<>(); |
| 52 | + for (Schema interfaceSchema : oneOfList) { |
| 53 | + if (StringUtils.isNotBlank(interfaceSchema.get$ref())) { |
| 54 | + String schemaName = OpenAPIUtil.getSimpleRef(interfaceSchema.get$ref()); |
| 55 | + modelNames.add(codegenConfig.toModelName(schemaName)); |
| 56 | + } |
| 57 | + } |
| 58 | + oneOfModel.vendorExtensions.put("x-model-names", modelNames); |
| 59 | + if (!modelNames.isEmpty()) { |
| 60 | + codegenModel.vendorExtensions.put("oneOf-model", oneOfModel); |
| 61 | + } |
| 62 | + } |
| 63 | + List<Schema> anyOfList = items.getAnyOf(); |
| 64 | + if (anyOfList != null && !anyOfList.isEmpty()){ |
| 65 | + String name = "AnyOf" + codegenModel.name + "Items"; |
| 66 | + final CodegenModel anyOfModel = createComposedModel(name); |
| 67 | + items.addExtension("x-model-name", codegenConfig.toModelName(name)); |
| 68 | + |
| 69 | + final List<String> modelNames = new ArrayList<>(); |
| 70 | + for (Schema interfaceSchema : anyOfList) { |
| 71 | + if (StringUtils.isNotBlank(interfaceSchema.get$ref())) { |
| 72 | + String schemaName = OpenAPIUtil.getSimpleRef(interfaceSchema.get$ref()); |
| 73 | + modelNames.add(codegenConfig.toModelName(schemaName)); |
| 74 | + } |
| 75 | + } |
| 76 | + anyOfModel.vendorExtensions.put("x-model-names", modelNames); |
| 77 | + if (!modelNames.isEmpty()) { |
| 78 | + codegenModel.vendorExtensions.put("anyOf-model", anyOfModel); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void configureOneOfModel(CodegenModel codegenModel, List<Schema> oneOf) { |
| 84 | + // no ops for html generator |
| 85 | + } |
| 86 | + |
| 87 | + public void configureAnyOfModel(CodegenModel codegenModel, List<Schema> anyOf) { |
| 88 | + // no ops for html generator |
| 89 | + } |
| 90 | + |
| 91 | + public void configureOneOfModelFromProperty(CodegenProperty codegenProperty, CodegenModel codegenModel) { |
| 92 | + // no ops for html generator |
| 93 | + } |
| 94 | + |
| 95 | + public void configureAnyOfModelFromProperty(CodegenProperty codegenProperty, CodegenModel codegenModel) { |
| 96 | + // no ops for html generator |
| 97 | + } |
| 98 | + |
| 99 | + private CodegenModel createFromOneOfSchemas(List<Schema> schemas) { |
| 100 | + final CodegenModel codegenModel = CodegenModelFactory.newInstance(CodegenModelType.MODEL); |
| 101 | + final List<String> modelNames = new ArrayList<>(); |
| 102 | + |
| 103 | + for (Schema interfaceSchema : schemas) { |
| 104 | + if (StringUtils.isNotBlank(interfaceSchema.get$ref())) { |
| 105 | + String schemaName = OpenAPIUtil.getSimpleRef(interfaceSchema.get$ref()); |
| 106 | + modelNames.add(codegenConfig.toModelName(schemaName)); |
| 107 | + } |
| 108 | + } |
| 109 | + codegenModel.vendorExtensions.put("x-model-names", modelNames); |
| 110 | + return codegenModel; |
| 111 | + } |
| 112 | + |
| 113 | + private CodegenModel createComposedModel(String name) { |
| 114 | + final CodegenModel composedModel = CodegenModelFactory.newInstance(CodegenModelType.MODEL); |
| 115 | + this.configureModel(composedModel, name); |
| 116 | + return composedModel; |
| 117 | + } |
| 118 | + |
| 119 | + private void configureModel(CodegenModel codegenModel, String name) { |
| 120 | + codegenModel.name = name; |
| 121 | + codegenModel.classname = codegenConfig.toModelName(name); |
| 122 | + codegenModel.classVarName = codegenConfig.toVarName(name); |
| 123 | + codegenModel.classFilename = codegenConfig.toModelFilename(name); |
| 124 | + codegenModel.vendorExtensions.put("x-is-composed-model", Boolean.TRUE); |
| 125 | + } |
| 126 | + |
| 127 | + private boolean hasNonObjectSchema(List<Schema> schemas) { |
| 128 | + for (Schema schema : schemas) { |
| 129 | + if (!codegenConfig.isObjectSchema(schema)) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + return false; |
| 134 | + } |
| 135 | +} |
0 commit comments