Skip to content

Commit ca03c47

Browse files
authored
Merge pull request #10204 from swagger-api/flattenInlineComposedSchema
Flatten inline composed schema
2 parents 3bc98d1 + 5f7c2fc commit ca03c47

File tree

5 files changed

+133
-6
lines changed

5 files changed

+133
-6
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/config/CodegenConfigurator.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class CodegenConfigurator implements Serializable {
5252

5353
private String lang;
5454
private String inputSpec;
55+
private boolean flattenInlineSchema;
5556
private String inputSpecURL;
5657
private String outputDir;
5758
private boolean verbose;
@@ -419,15 +420,15 @@ public CodegenConfigurator setIgnoreFileOverride(final String ignoreFileOverride
419420
this.ignoreFileOverride = ignoreFileOverride;
420421
return this;
421422
}
422-
423+
423424
public boolean isResolveFully() {
424425
return resolveFully;
425426
}
426427

427428
public CodegenConfigurator setResolveFully(boolean resolveFully) {
428429
this.resolveFully = resolveFully;
429430
return this;
430-
}
431+
}
431432

432433
public String loadSpecContent(String location, List<AuthorizationValue> auths) throws Exception{
433434
location = location.replaceAll("\\\\","/");
@@ -474,11 +475,11 @@ public ClientOptInput toClientOptInput() {
474475
config.setInputSpec(inputSpec);
475476
ParseOptions options = new ParseOptions();
476477
options.setResolve(true);
477-
options.setFlatten(true);
478478
options.setResolveFully(resolveFully);
479+
options.setFlatten(true);
480+
options.setFlattenComposedSchemas(flattenInlineSchema);
479481
SwaggerParseResult result = new OpenAPIParser().readContents(inputSpec, authorizationValues, options);
480482
OpenAPI openAPI = result.getOpenAPI();
481-
482483
if (config.needsUnflattenedSpec()) {
483484
ParseOptions optionsUnflattened = new ParseOptions();
484485
optionsUnflattened.setResolve(true);
@@ -510,8 +511,9 @@ public ClientOptInput toClientOptInput() {
510511
config.setInputURL(inputSpecURL);
511512
ParseOptions options = new ParseOptions();
512513
options.setResolve(true);
513-
options.setFlatten(true);
514514
options.setResolveFully(resolveFully);
515+
options.setFlatten(true);
516+
options.setFlattenComposedSchemas(flattenInlineSchema);
515517
SwaggerParseResult result = new OpenAPIParser().readLocation(inputSpecURL, authorizationValues, options);
516518
OpenAPI openAPI = result.getOpenAPI();
517519
LOGGER.debug("getClientOptInput - parsed inputSpecURL " + inputSpecURL);
@@ -652,4 +654,10 @@ public static CodegenConfigurator fromFile(String configFile) {
652654
return null;
653655
}
654656

657+
public boolean isFlattenInlineSchem() {
658+
return flattenInlineSchema;
659+
}
660+
public void setFlattenInlineSchema(boolean flattenInlineComposedSchemas) {
661+
this.flattenInlineSchema = flattenInlineComposedSchemas;
662+
}
655663
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/service/GeneratorUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public static ClientOptInput getClientOptInput(GenerationRequest generationReque
197197
configurator.setInputSpec(inputSpec);
198198
configurator.setInputSpecURL(inputSpecURL);
199199

200+
configurator.setFlattenInlineSchema(generationRequest.getOptions().isFlattenInlineComposedSchemas());
201+
200202
if (isNotEmpty(lang)) {
201203
configurator.setLang(lang);
202204
readCodegenArguments(configurator, options);

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/service/Options.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class Options {
4040

4141
private Map<String, String> codegenArguments = new LinkedHashMap<>();
4242

43+
private boolean flattenInlineComposedSchemas = false;
44+
4345
public Options authorizationValue(AuthorizationValue authorizationValue) {
4446
this.authorizationValue = authorizationValue;
4547
return this;
@@ -429,7 +431,7 @@ public Boolean getSkipOverride() {
429431
public void setSkipOverride(Boolean skipOverride) {
430432
this.skipOverride = skipOverride;
431433
}
432-
434+
433435
public Options resolveFully(Boolean resolveFully) {
434436
this.resolveFully = resolveFully;
435437
return this;
@@ -442,4 +444,15 @@ public Boolean getResolveFully() {
442444
public void setResolveFully(Boolean resolveFully) {
443445
this.resolveFully = resolveFully;
444446
}
447+
448+
public boolean isFlattenInlineComposedSchemas() {
449+
return flattenInlineComposedSchemas;
450+
}
451+
public void setFlattenInlineComposedSchema(boolean flattenInlineComposedSchemas) {
452+
this.flattenInlineComposedSchemas = flattenInlineComposedSchemas;
453+
}
454+
public Options flattenInlineComposedSchema(boolean flattenInlineComposedSchemas) {
455+
this.flattenInlineComposedSchemas = flattenInlineComposedSchemas;
456+
return this;
457+
}
445458
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/service/GeneratorServiceTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@
1818

1919
public class GeneratorServiceTest {
2020

21+
22+
@Test(description = "test generator oneOf ComposedSchema Properties")
23+
public void testGenerator_FlattenInlineComposedSchema() throws IOException {
24+
25+
String path = getTmpFolder().getAbsolutePath();
26+
GenerationRequest request = new GenerationRequest();
27+
request
28+
.codegenVersion(GenerationRequest.CodegenVersion.V3)
29+
.type(GenerationRequest.Type.CLIENT)
30+
.lang("java")
31+
.spec(loadSpecAsNode("3_0_0/FlattenComposedInlineSchema.yaml", true, false))
32+
.options(
33+
new Options()
34+
.flattenInlineComposedSchema(true)
35+
.outputDir(path)
36+
);
37+
38+
List<File> files = new GeneratorService().generationRequest(request).generate();
39+
Assert.assertFalse(files.isEmpty());
40+
for (File f: files) {
41+
String relPath = f.getAbsolutePath().substring(path.length());
42+
if ("/src/main/java/io/swagger/client/model/ContactbasemodelAllOf1.java".equals(relPath)) {
43+
Assert.assertTrue("/src/main/java/io/swagger/client/model/ContactbasemodelAllOf1.java".equals(relPath));
44+
}
45+
if ("/src/main/java/io/swagger/client/model/TestOneOf2.java".equals(relPath)) {
46+
Assert.assertTrue("/src/main/java/io/swagger/client/model/TestOneOf2.java".equals(relPath));
47+
}
48+
}
49+
50+
}
51+
2152
@Test(description = "test generator oneOf ComposedSchema Properties")
2253
public void testGenerator_OneOf_ComposedSchemaProperties() throws IOException {
2354

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
openapi: 3.0.2
2+
info:
3+
title: CC-20272 test - OAS3
4+
version: 1.0.0
5+
paths:
6+
/something:
7+
get:
8+
responses:
9+
200:
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Bar'
15+
components:
16+
schemas:
17+
Foo:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
Bar:
23+
type: object
24+
properties:
25+
foo1:
26+
description: An instance of Foo
27+
allOf:
28+
- $ref: '#/components/schemas/Foo'
29+
foo2:
30+
$ref: '#/components/schemas/Foo'
31+
Test:
32+
oneOf:
33+
- $ref: "#/components/schemas/Foo"
34+
- type: object
35+
properties:
36+
foo:
37+
type: string
38+
contact-base-model:
39+
allOf:
40+
- type: object
41+
required:
42+
- lastName
43+
- email
44+
properties:
45+
contactId:
46+
type: string
47+
readOnly: true
48+
fullName:
49+
type: string
50+
readOnly: true
51+
firstName:
52+
type: string
53+
lastName:
54+
type: string
55+
title:
56+
type: string
57+
email:
58+
type: string
59+
format: email
60+
passCode:
61+
type: string
62+
format: password
63+
indivId:
64+
type: string
65+
addresses:
66+
type: array
67+
items:
68+
$ref: '#/components/schemas/Foo'
69+
phones:
70+
type: array
71+
items:
72+
$ref: '#/components/schemas/Bar'
73+
- $ref: '#/components/schemas/Test'

0 commit comments

Comments
 (0)