Skip to content

Commit e3bbec4

Browse files
committed
fix html generator for composed schema properties
1 parent d71093d commit e3bbec4

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

src/main/java/io/swagger/codegen/v3/generators/html/StaticHtmlCodegen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class StaticHtmlCodegen extends DefaultCodegenConfig {
3232

3333
public StaticHtmlCodegen() {
3434
super();
35+
schemaHandler = new HtmlSchemaHandler(this);
3536
outputFolder = "docs";
3637

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

156157
/**
157158
* Convert Markdown text to HTML
158-
*
159+
*
159160
* @param input
160161
* text in Markdown; may be null.
161162
* @return the text, converted to Markdown. For null input, "" is returned.

src/main/resources/handlebars/htmlDocs/index.mustache

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,36 @@
174174
{{#example}}
175175
<div class="param-desc"><span class="param-type">example: {{example}}</span></div>
176176
{{/example}}
177+
{{#vendorExtensions.oneOf-model}}
178+
<div class="param-desc"><span class="param-type">oneOf:</span>
179+
{{#vendorExtensions.x-model-names}}
180+
<a href="#{{this}}"><code>{{this}}</code></a>&nbsp;&nbsp;&nbsp;
181+
{{/vendorExtensions.x-model-names}}
182+
</div>
183+
{{/vendorExtensions.oneOf-model}}
184+
{{#vendorExtensions.anyOf-model}}
185+
<div class="param-desc"><span class="param-type">anyOf:</span>
186+
{{#vendorExtensions.x-model-names}}
187+
<a href="#{{this}}"><code>{{this}}</code></a>&nbsp;&nbsp;&nbsp;
188+
{{/vendorExtensions.x-model-names}}
189+
</div>
190+
{{/vendorExtensions.anyOf-model}}
191+
{{#items}}
192+
{{#vendorExtensions.oneOf-model}}
193+
<div class="param-desc"><span class="param-type">items oneOf:</span>
194+
{{#vendorExtensions.x-model-names}}
195+
<a href="#{{this}}"><code>{{this}}</code></a>&nbsp;&nbsp;&nbsp;
196+
{{/vendorExtensions.x-model-names}}
197+
</div>
198+
{{/vendorExtensions.oneOf-model}}
199+
{{#vendorExtensions.anyOf-model}}
200+
<div class="param-desc"><span class="param-type">items anyOf:</span>
201+
{{#vendorExtensions.x-model-names}}
202+
<a href="#{{this}}"><code>{{this}}</code></a>&nbsp;&nbsp;&nbsp;
203+
{{/vendorExtensions.x-model-names}}
204+
</div>
205+
{{/vendorExtensions.anyOf-model}}
206+
{{/items}}
177207
{{/vars}}
178208
</div> <!-- field-items -->
179209
</div>

0 commit comments

Comments
 (0)