Skip to content

Commit 9b2d78a

Browse files
gracekarinafrantuma
authored andcommitted
adding flattenInlineComposedSchema as option for parser
1 parent 3bc98d1 commit 9b2d78a

File tree

5 files changed

+128
-4
lines changed

5 files changed

+128
-4
lines changed

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

Lines changed: 13 additions & 4 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,8 +475,9 @@ 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();
481483

@@ -510,8 +512,9 @@ public ClientOptInput toClientOptInput() {
510512
config.setInputURL(inputSpecURL);
511513
ParseOptions options = new ParseOptions();
512514
options.setResolve(true);
513-
options.setFlatten(true);
514515
options.setResolveFully(resolveFully);
516+
options.setFlatten(true);
517+
options.setFlattenComposedSchemas(flattenInlineSchema);
515518
SwaggerParseResult result = new OpenAPIParser().readLocation(inputSpecURL, authorizationValues, options);
516519
OpenAPI openAPI = result.getOpenAPI();
517520
LOGGER.debug("getClientOptInput - parsed inputSpecURL " + inputSpecURL);
@@ -652,4 +655,10 @@ public static CodegenConfigurator fromFile(String configFile) {
652655
return null;
653656
}
654657

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public class GenerationRequest implements Serializable {
1111
private Options options = new Options();
1212
private Type type;
1313
private CodegenVersion codegenVersion = CodegenVersion.V3;
14+
private boolean flattenInlineComposedSchemas = false;
15+
16+
public boolean isFlattenInlineComposedSchemas() {
17+
return flattenInlineComposedSchemas;
18+
}
1419

1520
public enum Type {
1621
CLIENT("client"), SERVER("server"), DOCUMENTATION("documentation"), CONFIG("config");
@@ -39,6 +44,11 @@ public enum CodegenVersion {
3944
V2, V3;
4045
}
4146

47+
public GenerationRequest flattenInlineComposedSchema(boolean flattenInlineComposedSchemas) {
48+
this.flattenInlineComposedSchemas = flattenInlineComposedSchemas;
49+
return this;
50+
}
51+
4252
public GenerationRequest lang(String lang) {
4353
this.lang = lang;
4454
return this;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public static ClientOptInput getClientOptInput(GenerationRequest generationReque
196196
configurator.setOutputDir(generationRequest.getOptions().getOutputDir());
197197
configurator.setInputSpec(inputSpec);
198198
configurator.setInputSpecURL(inputSpecURL);
199+
configurator.setFlattenInlineSchema(generationRequest.isFlattenInlineComposedSchemas());
199200

200201
if (isNotEmpty(lang)) {
201202
configurator.setLang(lang);

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+
.flattenInlineComposedSchema(true)
33+
.options(
34+
new Options()
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)