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

Commit e69bc3f

Browse files
committed
v2 generate component headers (#100)
* Adds CodegenHeader * Updates fromParameter * Moves setContent into fromParameter, simplifies fromParameter * Refactors fromParameter property assignment into helper method * Adds missing methods to generate header components * Updates param and header code to set correct style, updated header template param name * Adds OpenapiComponent interface * Fixes request body component docs to use modulePath * Uses modulePath in response docs * Has model docs use modulePath * Improves fromHeader usage * Fixes iterating over response headers * Refactors CodegenParameter toString, fixes header style * Regenerate sample, refed headers should not be generated inline * Adds refed headers in path response header and component response header * Adds imports for refed headers * Adds java code to generate header docs, CodegenResponse.responseHeaders renamed to headers * CodegenOperation.bodyParam changed to requestBody * Samples regenerated * Header doc files generated * Fixes link to component headers * Removes header details when there is a ref link * Fixes refed header links * Corrects header doc link * Fixes response component link to header in docs * Samples regenerated * Adds Int32JsonContentTypeHeader * Fixes one doc link to the new header * Fixes last remaining doc link to the header doc * Samples regenerated * Fixes Java tests
1 parent f970cb5 commit e69bc3f

File tree

270 files changed

+1582
-955
lines changed

Some content is hidden

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

270 files changed

+1582
-955
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.samskivert.mustache.Mustache.Compiler;
2121
import io.swagger.v3.oas.models.OpenAPI;
2222
import io.swagger.v3.oas.models.Operation;
23+
import io.swagger.v3.oas.models.headers.Header;
2324
import io.swagger.v3.oas.models.media.Schema;
2425
import io.swagger.v3.oas.models.parameters.RequestBody;
2526
import io.swagger.v3.oas.models.responses.ApiResponse;
@@ -83,6 +84,10 @@ public interface CodegenConfig {
8384

8485
String requestBodyDocFileFolder();
8586

87+
String headerFileFolder();
88+
89+
String headerDocFileFolder();
90+
8691
String modelPackage();
8792

8893
String packageName();
@@ -171,6 +176,10 @@ public interface CodegenConfig {
171176

172177
Map<String, String> requestBodyDocTemplateFiles();
173178

179+
Map<String, String> headerTemplateFiles();
180+
181+
Map<String, String> headerDocTemplateFiles();
182+
174183
Map<String, String> pathEndpointTemplateFiles();
175184

176185
Set<String> pathEndpointTestTemplateFiles();
@@ -231,6 +240,10 @@ public interface CodegenConfig {
231240

232241
String toResponseDocFilename(String componentName);
233242

243+
String toHeaderDocFilename(String componentName);
244+
245+
String toHeaderFilename(String componentName);
246+
234247
String toPathFileName(String path);
235248

236249
String toParameterFileName(String baseName);
@@ -388,4 +401,6 @@ public interface CodegenConfig {
388401
String getBodyParameterName(CodegenOperation co);
389402

390403
CodegenResponse fromResponse(ApiResponse response, String sourceJsonPath);
404+
405+
CodegenHeader fromHeader(Header parameter, String sourceJsonPath);
391406
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public class CodegenConstants {
3030
public static final String REQUEST_BODY_DOCS = "requestBodyDocs";
3131

3232
public static final String RESPONSES = "responses";
33+
34+
public static final String HEADERS = "headers";
35+
public static final String HEADER_DOCS = "headerDocs";
3336
public static final String SUPPORTING_FILES = "supportingFiles";
3437
public static final String MODEL_TESTS = "modelTests";
3538
public static final String MODEL_DOCS = "modelDocs";

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package org.openapitools.codegen;
22

33
import java.util.List;
4+
import java.util.Map;
45
import java.util.Objects;
56

67
public class CodegenEncoding {
78
private String contentType;
8-
private List<CodegenParameter> headers;
9+
private Map<String, CodegenHeader> headers;
910
private String style;
1011
private boolean explode;
1112
private boolean allowReserved;
1213

13-
public CodegenEncoding(String contentType, List<CodegenParameter> headers, String style, boolean explode, boolean allowReserved) {
14+
public CodegenEncoding(String contentType, Map<String, CodegenHeader> headers, String style, boolean explode, boolean allowReserved) {
1415
this.contentType = contentType;
1516
this.headers = headers;
1617
this.style = style;
@@ -22,7 +23,7 @@ public String getContentType() {
2223
return contentType;
2324
}
2425

25-
public List<CodegenParameter> getHeaders() {
26+
public Map<String, CodegenHeader> getHeaders() {
2627
return headers;
2728
}
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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 java.util.HashMap;
21+
import java.util.HashSet;
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
import java.util.Objects;
25+
import java.util.Set;
26+
27+
/**
28+
* Describes a single operation parameter in the OAS specification.
29+
* A unique parameter is defined by a combination of a name and location.
30+
* Parameters may be located in a path, query, header or cookie.
31+
*/
32+
public class CodegenHeader implements OpenapiComponent {
33+
public boolean isExplode;
34+
public String paramName,
35+
description, unescapedDescription, style;
36+
37+
public String nameInLowerCase; // property name in lower case
38+
public String example; // example value (x-example)
39+
public String jsonSchema;
40+
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
41+
public boolean isDeprecated;
42+
protected CodegenProperty schema;
43+
/**
44+
* Determines whether this parameter is mandatory. If the parameter is in "path",
45+
* this property is required and its value MUST be true. Otherwise, the property
46+
* MAY be included and its default value is false.
47+
*/
48+
public boolean required;
49+
protected boolean hasMultipleTypes = false;
50+
protected LinkedHashMap<String, CodegenMediaType> content;
51+
protected String ref;
52+
protected String refModule;
53+
54+
public Set<String> imports = new HashSet<String>();
55+
56+
protected String modulePath;
57+
58+
public CodegenHeader copy() {
59+
CodegenHeader output = new CodegenHeader();
60+
output.paramName = this.paramName;
61+
output.description = this.description;
62+
output.unescapedDescription = this.unescapedDescription;
63+
output.required = this.required;
64+
output.jsonSchema = this.jsonSchema;
65+
output.example = this.example;
66+
67+
if (this.content != null) {
68+
output.setContent(this.content);
69+
}
70+
if (this.schema != null) {
71+
output.setSchema(this.schema);
72+
}
73+
if (this.vendorExtensions != null) {
74+
output.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions);
75+
}
76+
if (this.ref != null) {
77+
output.setRef(this.ref);
78+
}
79+
if (this.refModule != null) {
80+
output.setRefModule(this.refModule);
81+
}
82+
if (this.imports != null) {
83+
output.imports = imports;
84+
}
85+
if (this.modulePath != null) {
86+
output.modulePath = modulePath;
87+
}
88+
output.isDeprecated = this.isDeprecated;
89+
output.isExplode = this.isExplode;
90+
output.style = this.style;
91+
92+
return output;
93+
}
94+
95+
public String getModulePath() {
96+
return modulePath;
97+
}
98+
99+
public void setModulePath(String modulePath) {
100+
this.modulePath = modulePath;
101+
}
102+
103+
@Override
104+
public int hashCode() {
105+
return Objects.hash(isExplode, paramName, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, hasMultipleTypes, schema, content, ref, refModule, imports, modulePath);
106+
}
107+
108+
@Override
109+
public boolean equals(Object o) {
110+
if (this == o) return true;
111+
if (!(o instanceof CodegenHeader)) return false;
112+
CodegenHeader that = (CodegenHeader) o;
113+
return isExplode == that.isExplode &&
114+
isDeprecated == that.isDeprecated &&
115+
required == that.required &&
116+
Objects.equals(modulePath, that.modulePath) &&
117+
Objects.equals(ref, that.getRef()) &&
118+
Objects.equals(imports, that.imports) &&
119+
Objects.equals(refModule, that.getRefModule()) &&
120+
Objects.equals(content, that.getContent()) &&
121+
Objects.equals(schema, that.getSchema()) &&
122+
Objects.equals(paramName, that.paramName) &&
123+
Objects.equals(description, that.description) &&
124+
Objects.equals(unescapedDescription, that.unescapedDescription) &&
125+
Objects.equals(style, that.style) &&
126+
Objects.equals(example, that.example) &&
127+
Objects.equals(jsonSchema, that.jsonSchema) &&
128+
Objects.equals(vendorExtensions, that.vendorExtensions);
129+
}
130+
131+
protected void addInstanceInfo(StringBuilder sb) {
132+
sb.append(", isExplode=").append(isExplode);
133+
sb.append(", paramName='").append(paramName).append('\'');
134+
sb.append(", description='").append(description).append('\'');
135+
sb.append(", unescapedDescription='").append(unescapedDescription).append('\'');
136+
sb.append(", style='").append(style).append('\'');
137+
sb.append(", example='").append(example).append('\'');
138+
sb.append(", jsonSchema='").append(jsonSchema).append('\'');
139+
sb.append(", vendorExtensions=").append(vendorExtensions);
140+
sb.append(", isDeprecated=").append(isDeprecated);
141+
sb.append(", required=").append(required);
142+
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
143+
sb.append(", schema=").append(schema);
144+
sb.append(", content=").append(content);
145+
sb.append(", ref=").append(ref);
146+
sb.append(", refModule=").append(refModule);
147+
sb.append(", imports=").append(imports);
148+
sb.append(", modulePath=").append(modulePath);
149+
}
150+
151+
@Override
152+
public String toString() {
153+
final StringBuilder sb = new StringBuilder("CodegenHeader{");
154+
addInstanceInfo(sb);
155+
sb.append('}');
156+
return sb.toString();
157+
}
158+
159+
public CodegenProperty getSchema() {
160+
return schema;
161+
}
162+
163+
public void setSchema(CodegenProperty schema) {
164+
this.schema = schema;
165+
}
166+
167+
public LinkedHashMap<String, CodegenMediaType> getContent() {
168+
return content;
169+
}
170+
171+
public void setContent(LinkedHashMap<String, CodegenMediaType> content) {
172+
this.content = content;
173+
}
174+
175+
176+
public String getRef() { return ref; }
177+
178+
public void setRef(String ref) { this.ref=ref; }
179+
180+
public String getRefModule() { return refModule; }
181+
182+
public void setRefModule(String refModule) { this.refModule=refModule; }
183+
}
184+

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* CodegenModel represents a schema object in a OpenAPI document.
2929
*/
3030
@JsonIgnoreProperties({"parentModel", "interfaceModels"})
31-
public class CodegenModel implements JsonSchema {
31+
public class CodegenModel implements JsonSchema, OpenapiComponent {
3232
// The parent model name from the schemas. The parent is determined by inspecting the allOf, anyOf and
3333
// oneOf attributes in the OAS. First codegen inspects 'allOf', then 'anyOf', then 'oneOf'.
3434
// If there are multiple object references in the attribute ('allOf', 'anyOf', 'oneOf'), and one of the
@@ -117,6 +117,7 @@ public class CodegenModel implements JsonSchema {
117117
private String format;
118118
private LinkedHashMap<String, List<String>> dependentRequired;
119119
private CodegenProperty contains;
120+
private String modulePath;
120121

121122
/**
122123
* The type of the value for the additionalProperties keyword in the OAS document.
@@ -175,6 +176,14 @@ public class CodegenModel implements JsonSchema {
175176
private String ref;
176177
private String refModule;
177178

179+
public String getModulePath() {
180+
return modulePath;
181+
}
182+
183+
public void setModulePath(String modulePath) {
184+
this.modulePath = modulePath;
185+
}
186+
178187
public String getAdditionalPropertiesType() {
179188
return additionalPropertiesType;
180189
}
@@ -1023,6 +1032,7 @@ public boolean equals(Object o) {
10231032
getUniqueItems() == that.getUniqueItems() &&
10241033
getExclusiveMinimum() == that.getExclusiveMinimum() &&
10251034
getExclusiveMaximum() == that.getExclusiveMaximum() &&
1035+
Objects.equals(modulePath, that.modulePath) &&
10261036
Objects.equals(contains, that.getContains()) &&
10271037
Objects.equals(dependentRequired, that.getDependentRequired()) &&
10281038
Objects.equals(format, that.getFormat()) &&
@@ -1104,7 +1114,7 @@ hasChildren, isMap, isDeprecated, hasOnlyReadOnly, getExternalDocumentation(), g
11041114
getAdditionalPropertiesIsAnyType(), hasDiscriminatorWithNonEmptyMapping,
11051115
isAnyType, getComposedSchemas(), hasMultipleTypes, isDecimal, isUuid, requiredVarsMap, ref,
11061116
uniqueItemsBoolean, schemaIsFromAdditionalProperties, isBooleanSchemaTrue, isBooleanSchemaFalse,
1107-
format, dependentRequired, contains, refModule);
1117+
format, dependentRequired, contains, refModule, modulePath);
11081118
}
11091119

11101120
@Override
@@ -1211,6 +1221,7 @@ public String toString() {
12111221
sb.append(", format=").append(format);
12121222
sb.append(", dependentRequired=").append(dependentRequired);
12131223
sb.append(", contains=").append(contains);
1224+
sb.append(", modulePath").append(modulePath);
12141225
sb.append('}');
12151226
return sb.toString();
12161227
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class CodegenOperation {
3232
summary, unescapedNotes, notes, baseName;
3333
public List<Map<String, String>> consumes, produces, prioritizedContentTypes;
3434
public List<CodegenServer> servers = new ArrayList<CodegenServer>();
35-
public CodegenParameter bodyParam;
35+
public CodegenParameter requestBody;
3636
public List<CodegenParameter> allParams = new ArrayList<CodegenParameter>();
3737
public List<CodegenParameter> bodyParams = new ArrayList<CodegenParameter>();
3838
public List<CodegenParameter> pathParams = new ArrayList<CodegenParameter>();
@@ -200,10 +200,10 @@ public boolean getAllResponsesAreErrors() {
200200
*/
201201
public Map<String, CodegenOperation> getContentTypeToOperation() {
202202
LinkedHashMap<String, CodegenOperation> contentTypeToOperation = new LinkedHashMap<>();
203-
if (bodyParam == null) {
203+
if (requestBody == null) {
204204
return null;
205205
}
206-
LinkedHashMap<String, CodegenMediaType> content = bodyParam.getContent();
206+
LinkedHashMap<String, CodegenMediaType> content = requestBody.getContent();
207207
for (String contentType: content.keySet()) {
208208
contentTypeToOperation.put(contentType, this);
209209
}
@@ -335,7 +335,7 @@ public String toString() {
335335
sb.append(", produces=").append(produces);
336336
sb.append(", prioritizedContentTypes=").append(prioritizedContentTypes);
337337
sb.append(", servers=").append(servers);
338-
sb.append(", bodyParam=").append(bodyParam);
338+
sb.append(", requestBody=").append(requestBody);
339339
sb.append(", allParams=").append(allParams);
340340
sb.append(", bodyParams=").append(bodyParams);
341341
sb.append(", pathParams=").append(pathParams);
@@ -401,7 +401,7 @@ public boolean equals(Object o) {
401401
Objects.equals(produces, that.produces) &&
402402
Objects.equals(prioritizedContentTypes, that.prioritizedContentTypes) &&
403403
Objects.equals(servers, that.servers) &&
404-
Objects.equals(bodyParam, that.bodyParam) &&
404+
Objects.equals(requestBody, that.requestBody) &&
405405
Objects.equals(allParams, that.allParams) &&
406406
Objects.equals(bodyParams, that.bodyParams) &&
407407
Objects.equals(pathParams, that.pathParams) &&
@@ -439,7 +439,7 @@ public int hashCode() {
439439
hasDefaultResponse, isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
440440
isRestful, isDeprecated, isCallbackRequest, uniqueItems, path, operationId, httpMethod,
441441
summary, unescapedNotes, notes, baseName, defaultResponse,
442-
consumes, produces, prioritizedContentTypes, servers, bodyParam, allParams, bodyParams,
442+
consumes, produces, prioritizedContentTypes, servers, requestBody, allParams, bodyParams,
443443
pathParams, queryParams, headerParams, formParams, cookieParams, requiredParams, optionalParams,
444444
authMethods, tags, responses, callbacks, imports, examples, requestBodyExamples, externalDocs,
445445
vendorExtensions, nickname, operationIdOriginal, operationIdLowerCase, operationIdCamelCase,

0 commit comments

Comments
 (0)