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

Commit 8c01aac

Browse files
authored
v2 adds and uses CodegenRefInfo class to consolidate $ref info: ref/refClass/refModule (#118)
* Adds CodegenRefInfo * Adds needed base classes * Renames class to OpenApiLocation * Adds getName and setName to OpenApiLocation * Template usages of ref replaces with ref.getRef * ref property changed to refInfo * Fixes refInfo usages in Java * Templates updated ref -> refInfo * Fixes another refInfo java location * Template updates, refModule -> refInfo.refModule * Templates updated, refClass -> refInfo.refClass * Deletes refModule + refClass properties, they are in refInfo * Fixes other needed java locations * Fixes java tests, only sets refInfo if ref not null, only set name if currentJsonPath not null * Adds code to setComponentModule * Fixes java test * Samples regen * Adds refInfo printing and equality * Adds hash and equals methods to CodegenRefInfo * Fixes log line when generating
1 parent 55ef00b commit 8c01aac

File tree

50 files changed

+699
-574
lines changed

Some content is hidden

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

50 files changed

+699
-574
lines changed

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

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,18 @@
1717

1818
package org.openapitools.codegen;
1919

20-
import org.openapitools.codegen.utils.ModelUtils;
21-
22-
import java.util.Map;
2320
import java.util.Objects;
2421

2522
/**
2623
* Describes a single operation parameter in the OAS specification.
2724
* A unique parameter is defined by a combination of a name and location.
2825
* Parameters may be located in a path, query, header or cookie.
2926
*/
30-
public class CodegenHeader extends CodegenRequestBody {
31-
public boolean isExplode;
32-
public String style;
33-
34-
public boolean isDeprecated;
35-
protected CodegenSchema schema;
36-
37-
public CodegenSchema getSetSchema() {
38-
if (schema != null) {
39-
return schema;
40-
}
41-
if (content != null) {
42-
for (CodegenMediaType codegenMediaType: content.values()) {
43-
return codegenMediaType.getSchema();
44-
}
45-
}
46-
return null;
47-
}
48-
49-
public String getSetSchemaJsonPath(String jsonPath) {
50-
if (schema != null) {
51-
return jsonPath + "/schema";
52-
}
53-
if (content != null) {
54-
for (Map.Entry<String, CodegenMediaType> entry: content.entrySet()) {
55-
if (entry.getValue().getSchema() != null) {
56-
String contentType = entry.getKey();
57-
return jsonPath + "/content/" + ModelUtils.encodeSlashes(contentType) + "/schema";
58-
}
59-
}
60-
}
61-
return null;
62-
}
27+
public class CodegenHeader extends CodegenHeaderBase implements OpenApiLocation<CodegenHeader> {
28+
protected CodegenRefInfo<CodegenHeader> refInfo;
6329
@Override
6430
public int hashCode() {
65-
return Objects.hash(refClass, name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
31+
return Objects.hash(name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, refInfo, imports, componentModule);
6632
}
6733

6834
@Override
@@ -71,18 +37,12 @@ public boolean equals(Object o) {
7137
if (!(o instanceof CodegenHeader)) return false;
7238
if (! super.equals(o)) return false;
7339
CodegenHeader that = (CodegenHeader) o;
74-
return isExplode == that.isExplode &&
75-
isDeprecated == that.isDeprecated &&
76-
Objects.equals(schema, that.getSchema()) &&
77-
Objects.equals(style, that.style);
40+
return Objects.equals(refInfo, that.refInfo);
7841
}
7942

8043
protected void addInstanceInfo(StringBuilder sb) {
8144
super.addInstanceInfo(sb);
82-
sb.append(", isExplode=").append(isExplode);
83-
sb.append(", style='").append(style).append('\'');
84-
sb.append(", isDeprecated=").append(isDeprecated);
85-
sb.append(", schema=").append(schema);
45+
sb.append(", refInfo=").append(refInfo);
8646
}
8747

8848
@Override
@@ -93,15 +53,8 @@ public String toString() {
9353
return sb.toString();
9454
}
9555

96-
@Override
97-
public CodegenHeader getRef() { return (CodegenHeader) ref; }
56+
public CodegenRefInfo<CodegenHeader> getRefInfo() { return refInfo; }
9857

99-
public CodegenSchema getSchema() {
100-
return schema;
101-
}
102-
103-
public void setSchema(CodegenSchema schema) {
104-
this.schema = schema;
105-
}
58+
public void setRefInfo(CodegenRefInfo<CodegenHeader> refInfo) { this.refInfo = refInfo; }
10659
}
10760

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
* Copyright 2018 SmartBear Software
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.openapitools.codegen;
19+
20+
import org.openapitools.codegen.utils.ModelUtils;
21+
22+
import java.util.Map;
23+
import java.util.Objects;
24+
25+
/**
26+
* Describes a single operation parameter in the OAS specification.
27+
* A unique parameter is defined by a combination of a name and location.
28+
* Parameters may be located in a path, query, header or cookie.
29+
*/
30+
abstract class CodegenHeaderBase extends CodegenRequestBodyBase {
31+
public boolean isExplode;
32+
public String style;
33+
public boolean isDeprecated;
34+
protected CodegenSchema schema;
35+
36+
public CodegenSchema getSetSchema() {
37+
if (schema != null) {
38+
return schema;
39+
}
40+
if (content != null) {
41+
for (CodegenMediaType codegenMediaType: content.values()) {
42+
return codegenMediaType.getSchema();
43+
}
44+
}
45+
return null;
46+
}
47+
48+
public String getSetSchemaJsonPath(String jsonPath) {
49+
if (schema != null) {
50+
return jsonPath + "/schema";
51+
}
52+
if (content != null) {
53+
for (Map.Entry<String, CodegenMediaType> entry: content.entrySet()) {
54+
if (entry.getValue().getSchema() != null) {
55+
String contentType = entry.getKey();
56+
return jsonPath + "/content/" + ModelUtils.encodeSlashes(contentType) + "/schema";
57+
}
58+
}
59+
}
60+
return null;
61+
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (this == o) return true;
66+
if (!(o instanceof CodegenHeaderBase)) return false;
67+
if (! super.equals(o)) return false;
68+
CodegenHeaderBase that = (CodegenHeaderBase) o;
69+
return isExplode == that.isExplode &&
70+
isDeprecated == that.isDeprecated &&
71+
Objects.equals(schema, that.getSchema()) &&
72+
Objects.equals(style, that.style);
73+
}
74+
75+
protected void addInstanceInfo(StringBuilder sb) {
76+
super.addInstanceInfo(sb);
77+
sb.append(", isExplode=").append(isExplode);
78+
sb.append(", style='").append(style).append('\'');
79+
sb.append(", isDeprecated=").append(isDeprecated);
80+
sb.append(", schema=").append(schema);
81+
}
82+
83+
public CodegenSchema getSchema() {
84+
return schema;
85+
}
86+
87+
public void setSchema(CodegenSchema schema) {
88+
this.schema = schema;
89+
}
90+
}
91+

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class CodegenOperation {
3939
public List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>();
4040
public List<CodegenParameter> implicitHeadersParams = new ArrayList<CodegenParameter>();
4141
public List<CodegenParameter> cookieParams = new ArrayList<CodegenParameter>();
42-
public List<CodegenRequestBody> requiredParams = new ArrayList<CodegenRequestBody>();
43-
public List<CodegenRequestBody> optionalParams = new ArrayList<CodegenRequestBody>();
42+
public List<CodegenRequestBodyBase> requiredParams = new ArrayList<CodegenRequestBodyBase>();
43+
public List<CodegenRequestBodyBase> optionalParams = new ArrayList<CodegenRequestBodyBase>();
4444
public List<CodegenSecurity> authMethods;
4545
public Map<String, CodegenTag> tags;
4646
public TreeMap<String, CodegenResponse> responses = null;
@@ -176,9 +176,9 @@ public Map<String, CodegenOperation> getContentTypeToOperation() {
176176
return null;
177177
}
178178
LinkedHashMap<String, CodegenMediaType> content;
179-
CodegenRequestBody ref = (CodegenRequestBody) requestBody.getRef();
180-
if (ref != null) {
181-
content = ref.getContent();
179+
CodegenRefInfo<CodegenRequestBody> refInfo = requestBody.getRefInfo();
180+
if (refInfo != null) {
181+
content = refInfo.getRef().getContent();
182182
} else {
183183
content = requestBody.getContent();
184184
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@
2424
* A unique parameter is defined by a combination of a name and location.
2525
* Parameters may be located in a path, query, header or cookie.
2626
*/
27-
public class CodegenParameter extends CodegenHeader {
27+
public class CodegenParameter extends CodegenHeaderBase implements OpenApiLocation<CodegenParameter> {
2828
public boolean isFormParam, isQueryParam, isPathParam, isHeaderParam,
2929
isCookieParam, isBodyParam, isAllowEmptyValue, isDeepObject;
3030
// stores the openapi name property
3131
public String baseName;
32+
protected CodegenRefInfo<CodegenParameter> refInfo;
3233

33-
public CodegenParameter getRef() { return (CodegenParameter) ref; }
34+
public CodegenRefInfo<CodegenParameter> getRefInfo() { return refInfo; }
35+
36+
public void setRefInfo(CodegenRefInfo<CodegenParameter> refInfo) { this.refInfo = refInfo; }
3437

3538
@Override
3639
public int hashCode() {
37-
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);
40+
return Objects.hash(name, isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isExplode, baseName, description, unescapedDescription, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, refInfo, imports, componentModule);
3841
}
3942

4043
@Override
@@ -49,6 +52,7 @@ public boolean equals(Object o) {
4952
isHeaderParam == that.isHeaderParam &&
5053
isCookieParam == that.isCookieParam &&
5154
isBodyParam == that.isBodyParam &&
55+
Objects.equals(refInfo, that.refInfo) &&
5256
Objects.equals(baseName, that.baseName) &&
5357
Objects.equals(isDeepObject, that.isDeepObject);
5458
}
@@ -64,6 +68,7 @@ protected void addInstanceInfo(StringBuilder sb) {
6468
sb.append(", deepObject='").append(isDeepObject).append('\'');
6569
sb.append(", allowEmptyValue='").append(isAllowEmptyValue).append('\'');
6670
sb.append(", baseName='").append(baseName).append('\'');
71+
sb.append(", refInfo='").append(refInfo).append('\'');
6772
}
6873

6974
@Override
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.openapitools.codegen;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
import java.util.Objects;
6+
7+
@JsonIgnoreProperties({"ref"})
8+
public class CodegenRefInfo<T> {
9+
private T ref;
10+
private String refClass;
11+
private String refModule;
12+
13+
protected void addInstanceInfo(StringBuilder sb) {
14+
sb.append("refModule=").append(refModule);
15+
sb.append(", refClass=").append(refClass);
16+
}
17+
18+
@Override
19+
public String toString() {
20+
final StringBuilder sb = new StringBuilder("CodegenRefInfo{");
21+
addInstanceInfo(sb);
22+
sb.append('}');
23+
return sb.toString();
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
// ref must be omitted here for generation to work
29+
return Objects.hash(refClass, refModule);
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) return true;
35+
if (!(o instanceof CodegenRefInfo)) return false;
36+
CodegenRefInfo that = (CodegenRefInfo) o;
37+
return Objects.equals(refModule, that.refModule) &&
38+
Objects.equals(refClass, that.refClass) &&
39+
Objects.equals(ref, that.ref);
40+
}
41+
42+
public CodegenRefInfo(T ref, String refClass, String refModule) {
43+
this.ref = ref;
44+
this.refClass = refClass;
45+
this.refModule = refModule ;
46+
}
47+
48+
public T getRef() { return ref; }
49+
public String getRefClass() { return refClass; }
50+
public String getRefModule() { return refModule; }
51+
}

0 commit comments

Comments
 (0)