Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit e82ba0c

Browse files
authored
v2 use python classes to hold non-schema components (#110)
* Adds refClass to all components * Updates requestBody templates to use name.getCamelCaseName and write out a class * RequestBody converted to class, petstore sample regenerated * Fixes python tests * Uses response class in response modules * Adds generic parameter to api_client.OpenApiResponse base class * Adds spaces in response class property definitions * Updates headers and parameters to write classes * Petstore regnerated with param + header classses, endpoint not yet fixed * Uses classes in parameters and headers * Fixes header and prameter refClasses * Fixes many python tests * Fixes python tests * Samples regenerated * Removes java instance copy code and usage
1 parent 8998e9a commit e82ba0c

File tree

820 files changed

+3673
-4750
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

820 files changed

+3673
-4750
lines changed

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public interface CodegenConfig {
394394

395395
boolean getAddSuffixToDuplicateOperationNicknames();
396396

397-
String toRefClass(String ref, String sourceJsonPath);
397+
String toRefClass(String ref, String sourceJsonPath, String expectedComponentType);
398398

399399
CodegenRequestBody fromRequestBody(RequestBody body, String sourceJsonPath);
400400

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/CodegenHeader.java

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,6 @@ public class CodegenHeader extends CodegenRequestBody {
3838
public boolean isDeprecated;
3939
protected CodegenSchema schema;
4040

41-
public CodegenHeader copy() {
42-
CodegenHeader output = new CodegenHeader();
43-
output.description = this.description;
44-
output.unescapedDescription = this.unescapedDescription;
45-
output.required = this.required;
46-
output.jsonSchema = this.jsonSchema;
47-
output.example = this.example;
48-
49-
if (this.name != null) {
50-
output.setName(this.name);
51-
}
52-
if (this.content != null) {
53-
output.setContent(this.content);
54-
}
55-
if (this.schema != null) {
56-
output.setSchema(this.schema);
57-
}
58-
if (this.vendorExtensions != null) {
59-
output.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions);
60-
}
61-
if (this.ref != null) {
62-
output.setRef(this.ref);
63-
}
64-
if (this.refModule != null) {
65-
output.setRefModule(this.refModule);
66-
}
67-
if (this.imports != null) {
68-
output.imports = imports;
69-
}
70-
if (this.componentModule != null) {
71-
output.componentModule = componentModule;
72-
}
73-
output.isDeprecated = this.isDeprecated;
74-
output.isExplode = this.isExplode;
75-
output.style = this.style;
76-
77-
return output;
78-
}
79-
8041
public CodegenSchema getSetSchema() {
8142
if (schema != null) {
8243
return schema;
@@ -105,7 +66,7 @@ public String getSetSchemaJsonPath(String jsonPath) {
10566
}
10667
@Override
10768
public int hashCode() {
108-
return Objects.hash(name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
69+
return Objects.hash(refClass, name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
10970
}
11071

11172
@Override

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,57 +30,9 @@ public class CodegenParameter extends CodegenHeader {
3030
// stores the openapi name property
3131
public String baseName;
3232

33-
public CodegenParameter copy() {
34-
CodegenParameter output = new CodegenParameter();
35-
output.baseName = this.baseName;
36-
output.description = this.description;
37-
output.unescapedDescription = this.unescapedDescription;
38-
output.isFormParam = this.isFormParam;
39-
output.isQueryParam = this.isQueryParam;
40-
output.isPathParam = this.isPathParam;
41-
output.isHeaderParam = this.isHeaderParam;
42-
output.isCookieParam = this.isCookieParam;
43-
output.isBodyParam = this.isBodyParam;
44-
output.required = this.required;
45-
output.jsonSchema = this.jsonSchema;
46-
output.example = this.example;
47-
48-
if (this.name != null) {
49-
output.name = this.name;
50-
}
51-
if (this.content != null) {
52-
output.setContent(this.content);
53-
}
54-
if (this.schema != null) {
55-
output.setSchema(this.schema);
56-
}
57-
if (this.vendorExtensions != null) {
58-
output.vendorExtensions = new HashMap<>(this.vendorExtensions);
59-
}
60-
if (this.ref != null) {
61-
output.setRef(this.ref);
62-
}
63-
if (this.refModule != null) {
64-
output.setRefModule(this.refModule);
65-
}
66-
if (this.imports != null) {
67-
output.imports = imports;
68-
}
69-
if (this.componentModule != null) {
70-
output.componentModule = componentModule;
71-
}
72-
output.isDeprecated = this.isDeprecated;
73-
output.isExplode = this.isExplode;
74-
output.style = this.style;
75-
output.isDeepObject = this.isDeepObject;
76-
output.isAllowEmptyValue = this.isAllowEmptyValue;
77-
78-
return output;
79-
}
80-
8133
@Override
8234
public int hashCode() {
83-
return Objects.hash(name, isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isExplode, baseName, description, unescapedDescription, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
35+
return Objects.hash(refClass, name, isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isExplode, baseName, description, unescapedDescription, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
8436
}
8537

8638
@Override

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/CodegenRequestBody.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class CodegenRequestBody implements OpenapiComponent {
4949
protected String refModule;
5050
protected Set<String> imports = new HashSet<String>();
5151
protected String componentModule;
52+
protected String refClass;
5253

5354
public String getComponentModule() {
5455
return componentModule;
@@ -58,9 +59,17 @@ public void setComponentModule(String componentModule) {
5859
this.componentModule = componentModule;
5960
}
6061

62+
public String getRefClass() {
63+
return refClass;
64+
}
65+
66+
public void setRefClass(String refClass) {
67+
this.refClass = refClass;
68+
}
69+
6170
@Override
6271
public int hashCode() {
63-
return Objects.hash(description, unescapedDescription, name, example, jsonSchema, vendorExtensions, required, content, ref, refModule, imports, componentModule);
72+
return Objects.hash(refClass, description, unescapedDescription, name, example, jsonSchema, vendorExtensions, required, content, ref, refModule, imports, componentModule);
6473
}
6574

6675
@Override
@@ -69,6 +78,7 @@ public boolean equals(Object o) {
6978
if (!(o instanceof CodegenRequestBody)) return false;
7079
CodegenRequestBody that = (CodegenRequestBody) o;
7180
return required == that.required &&
81+
Objects.equals(refClass, that.refClass) &&
7282
Objects.equals(name, that.name) &&
7383
Objects.equals(componentModule, that.componentModule) &&
7484
Objects.equals(ref, that.getRef()) &&
@@ -93,6 +103,7 @@ protected void addInstanceInfo(StringBuilder sb) {
93103
sb.append(", content=").append(content);
94104
sb.append(", ref=").append(ref);
95105
sb.append(", refModule=").append(refModule);
106+
sb.append(", refClass=").append(refClass);
96107
sb.append(", imports=").append(imports);
97108
sb.append(", componentModule=").append(componentModule);
98109
}

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/CodegenResponse.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public class CodegenResponse implements OpenapiComponent {
3030
private String ref;
3131
public Set<String> imports = new TreeSet<>();
3232
private String refModule;
33+
private String refClass;
3334
private String componentModule;
3435

3536
@Override
3637
public int hashCode() {
37-
return Objects.hash(name, message, examples,
38+
return Objects.hash(refClass, name, message, examples,
3839
jsonSchema, vendorExtensions,
3940
headers, content,
4041
ref, imports, refModule, componentModule);
@@ -46,6 +47,7 @@ public boolean equals(Object o) {
4647
if (!(o instanceof CodegenResponse)) return false;
4748
CodegenResponse that = (CodegenResponse) o;
4849
return Objects.equals(name, that.name) &&
50+
Objects.equals(refClass, that.refClass) &&
4951
Objects.equals(imports, that.imports) &&
5052
Objects.equals(ref, that.getRef()) &&
5153
Objects.equals(content, that.getContent()) &&
@@ -94,6 +96,7 @@ public String toString() {
9496
sb.append(", content=").append(content);
9597
sb.append(", ref=").append(ref);
9698
sb.append(", refModule=").append(refModule);
99+
sb.append(", refClass=").append(refClass);
97100
sb.append(", imports=").append(imports);
98101
sb.append(", componentModule=").append(componentModule);
99102
sb.append('}');
@@ -108,6 +111,14 @@ public String toString() {
108111

109112
public void setRefModule(String refModule) { this.refModule=refModule; }
110113

114+
public String getRefClass() {
115+
return refClass;
116+
}
117+
118+
public void setRefClass(String refClass) {
119+
this.refClass = refClass;
120+
}
121+
111122
public CodegenKey getName() { return name; }
112123

113124
public void setName(CodegenKey name) { this.name=name; }

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
24582458
LOGGER.warn("'{}' defines discriminator '{}', but the referenced schema '{}' is incorrect. {}",
24592459
composedSchemaName, discPropName, modelName, msgSuffix);
24602460
}
2461-
MappedModel mm = new MappedModel(modelName, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath));
2461+
MappedModel mm = new MappedModel(modelName, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath, "schemas"));
24622462
descendentSchemas.add(mm);
24632463
Schema cs = ModelUtils.getSchema(openAPI, modelName);
24642464
if (cs == null) { // cannot lookup the model based on the name
@@ -2467,7 +2467,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
24672467
Map<String, Object> vendorExtensions = cs.getExtensions();
24682468
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
24692469
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
2470-
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath));
2470+
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath, "schemas"));
24712471
descendentSchemas.add(mm);
24722472
}
24732473
}
@@ -2520,21 +2520,21 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
25202520
break;
25212521
}
25222522
currentSchemaName = queue.remove(0);
2523-
MappedModel mm = new MappedModel(currentSchemaName, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath));
2523+
MappedModel mm = new MappedModel(currentSchemaName, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath, "schemas"));
25242524
descendentSchemas.add(mm);
25252525
Schema cs = schemas.get(currentSchemaName);
25262526
Map<String, Object> vendorExtensions = cs.getExtensions();
25272527
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
25282528
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
2529-
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath));
2529+
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath, "schemas"));
25302530
descendentSchemas.add(mm);
25312531
}
25322532
}
25332533
return descendentSchemas;
25342534
}
25352535

2536-
protected String getRefClassWithModule(String ref, String sourceJsonPath) {
2537-
String refClass = toRefClass(ref, sourceJsonPath);
2536+
protected String getRefClassWithModule(String ref, String sourceJsonPath, String expectedComponentType) {
2537+
String refClass = toRefClass(ref, sourceJsonPath, expectedComponentType);
25382538
return refClass;
25392539
}
25402540

@@ -2572,11 +2572,11 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
25722572
if (ModelUtils.getSchema(openAPI, name) == null) {
25732573
LOGGER.error("Failed to lookup the schema '{}' when processing the discriminator mapping of oneOf/anyOf. Please check to ensure it's defined properly.", name);
25742574
} else {
2575-
modelName = getRefClassWithModule(e.getValue(), sourceJsonPath);
2575+
modelName = getRefClassWithModule(e.getValue(), sourceJsonPath, "schemas");
25762576
}
25772577
} else {
25782578
String ref = "#/components/schemas/" + value;
2579-
modelName = getRefClassWithModule(ref, sourceJsonPath);
2579+
modelName = getRefClassWithModule(ref, sourceJsonPath, "schemas");
25802580
}
25812581
if (modelName != null) {
25822582
uniqueDescendants.add(new MappedModel(e.getKey(), modelName));
@@ -3087,9 +3087,10 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
30873087
property.setRef(ref);
30883088
property.setRefClass(toRefClass(
30893089
ref,
3090-
sourceJsonPath
3090+
sourceJsonPath,
3091+
"schemas"
30913092
));
3092-
property.setRefModule(toRefModule(ref, "schemas", sourceJsonPath));
3093+
property.setRefModule(toRefModule(ref, sourceJsonPath, "schemas"));
30933094
}
30943095
String example = toExampleValue(p);
30953096
property.setExample(example);
@@ -3103,7 +3104,7 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
31033104
return property;
31043105
}
31053106

3106-
public String toRefClass(String ref, String sourceJsonPath) {
3107+
public String toRefClass(String ref, String sourceJsonPath, String expectedComponentType) {
31073108
String[] refPieces = ref.split("/");
31083109
return toModelName(refPieces[refPieces.length-1]);
31093110
}
@@ -3324,13 +3325,13 @@ public CodegenOperation fromOperation(String path,
33243325
i++;
33253326

33263327
if (p.isQueryParam) {
3327-
queryParams.add(p.copy());
3328+
queryParams.add(p);
33283329
} else if (p.isPathParam) {
3329-
pathParams.add(p.copy());
3330+
pathParams.add(p);
33303331
} else if (p.isHeaderParam) {
3331-
headerParams.add(p.copy());
3332+
headerParams.add(p);
33323333
} else if (p.isCookieParam) {
3333-
cookieParams.add(p.copy());
3334+
cookieParams.add(p);
33343335
} else {
33353336
LOGGER.warn("Unknown parameter type for {}", p.baseName);
33363337
}
@@ -3341,9 +3342,9 @@ public CodegenOperation fromOperation(String path,
33413342
// create optional, required parameters
33423343
for (CodegenParameter cp : allParams) {
33433344
if (cp.required) { //required parameters
3344-
requiredParams.add(cp.copy());
3345+
requiredParams.add(cp);
33453346
} else { // optional parameters
3346-
optionalParams.add(cp.copy());
3347+
optionalParams.add(cp);
33473348
}
33483349
}
33493350

@@ -5024,7 +5025,7 @@ public String toRequestBodyFilename(String componentName) {
50245025
return toModuleFilename(componentName);
50255026
}
50265027

5027-
protected String toRefModule(String ref, String expectedComponentType, String sourceJsonPath) {
5028+
protected String toRefModule(String ref, String sourceJsonPath, String expectedComponentType) {
50285029
// ref #/components/schemas/SomeModel -> some_model
50295030
// ref #/components/requestBodies/SomeBody -> some_body
50305031
// ref #/components/parameters/SomeParam -> some_param
@@ -5062,8 +5063,10 @@ protected String toRefModule(String ref, String expectedComponentType, String so
50625063
private void setLocationInfo(String ref, OpenapiComponent instance, String sourceJsonPath, String expectedComponentType) {
50635064
if (ref != null) {
50645065
instance.setRef(ref);
5065-
String refModule = toRefModule(ref, expectedComponentType, sourceJsonPath);
5066+
String refModule = toRefModule(ref, sourceJsonPath, expectedComponentType);
50665067
instance.setRefModule(refModule);
5068+
String refClass = toRefClass(ref, sourceJsonPath, expectedComponentType);
5069+
instance.setRefClass(refClass);
50675070
}
50685071
String[] refPieces = sourceJsonPath.split("/");
50695072
if (sourceJsonPath.startsWith("#/components/") && refPieces.length == 4) {

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/OpenapiComponent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ public interface OpenapiComponent {
3737

3838
void setRefModule(String refModule);
3939

40-
// TODO add refClass here when all components use classes in their python definition
40+
String getRefClass();
41+
42+
void setRefClass(String refClass);
4143
}

0 commit comments

Comments
 (0)