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

Commit c9fcff6

Browse files
committed
v2 generates components.responses (#97)
* Fixes java tests * Adds code to generate response modules * Adds response component * Refactor, eliminates response code, unused java props and methods removed * Fixes java ref to vars that are gone, creates template * Regen sample, fixes endpoint method type hints * Endpoint doc template update * Template typo fix * Uses integers for status codes and wildcard keys, sample regen * Sample regen * Adds printing of response back in * nulls response maps initially * Fixes response body type hints in docs * Fixes endpoint docs * Fixes response code reference in endpoint docs * Fixes endpoint test http code, fixes some java tests * Adds component responses to readme * Removes unused java code * Uses refed responses if they exist * Removes generation of responses when they are referenced * deletes paths and regens it * Template and java update * Samples regenerated with doc links to component responses * Samples regen to include component links * Fixes link to component response * Adds geenration component response inline headers * Sammple updated to include component response with refed content schema * Sample regen and test template tweaked * Fixes component response anchors for headers and body * Fixes schema links in component response * Fixes imports for CodegenResponse * Adds response with inline content and header definition * Sample regenerated * Readme links updated * Simplifies doc link generation * Fixes links * Fixes module structure of component responses, adds init file * Samples regenerated * Fixes tests in DefaultCodegenTest * Fixes java tests in JavaModelTest * Disables failing test * Fixes schema links in requestbody docs * Samples regenerated * Regen samples with better endpoint test spacing
1 parent 21c79e5 commit c9fcff6

File tree

1,330 files changed

+2771
-4310
lines changed

Some content is hidden

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

1,330 files changed

+2771
-4310
lines changed

modules/openapi-json-schema-generator-cli/src/test/java/org/openapitools/codegen/cmd/GenerateTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,12 @@ public void testVerboseShort() {
433433
verifyNoMoreInteractions(configurator);
434434
}
435435

436-
/**
437-
* This test ensures that when the
438-
*/
439-
@Test(expectedExceptions = SpecValidationException.class)
440-
public void testNPEWithInvalidSpecFile() {
441-
setupAndRunTest("-i", "src/test/resources/npe-test.yaml", "-g", "java", "-o", "src/main/java", false, null);
442-
}
436+
// /**
437+
// * This test ensures that a NPE is thrown
438+
// * Test stopped passing when swagger-parser setResolve was switched to false
439+
// */
440+
// @Test(expectedExceptions = SpecValidationException.class)
441+
// public void testNPEWithInvalidSpecFile() {
442+
// setupAndRunTest("-i", "src/test/resources/npe-test.yaml", "-g", "java", "-o", "src/main/java", false, null);
443+
// }
443444
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.swagger.v3.oas.models.Operation;
2323
import io.swagger.v3.oas.models.media.Schema;
2424
import io.swagger.v3.oas.models.parameters.RequestBody;
25+
import io.swagger.v3.oas.models.responses.ApiResponse;
2526
import io.swagger.v3.oas.models.security.SecurityScheme;
2627
import io.swagger.v3.oas.models.servers.Server;
2728
import io.swagger.v3.oas.models.servers.ServerVariable;
@@ -88,6 +89,10 @@ public interface CodegenConfig {
8889

8990
String requestBodyFileFolder();
9091

92+
String responseFileFolder(String componentName);
93+
94+
String responseDocFileFolder();
95+
9196
String toApiName(String name);
9297

9398
String toApiVarName(String name);
@@ -180,6 +185,10 @@ public interface CodegenConfig {
180185

181186
Set<String> pathEndpointResponseHeaderTemplateFiles();
182187

188+
Map<String, String> responseTemplateFiles();
189+
190+
Map<String, String> responseDocTemplateFiles();
191+
183192
Map<String, String> apiTestTemplateFiles();
184193

185194
Map<String, String> modelTestTemplateFiles();
@@ -220,6 +229,8 @@ public interface CodegenConfig {
220229

221230
String toRequestBodyDocFilename(String componentName);
222231

232+
String toResponseDocFilename(String componentName);
233+
223234
String toPathFileName(String path);
224235

225236
String toParameterFileName(String baseName);
@@ -375,4 +386,6 @@ public interface CodegenConfig {
375386
CodegenParameter fromRequestBody(RequestBody body, String bodyParameterName, String sourceJsonPath);
376387

377388
String getBodyParameterName(CodegenOperation co);
389+
390+
CodegenResponse fromResponse(ApiResponse response, String sourceJsonPath);
378391
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class CodegenConstants {
2828

2929
public static final String REQUEST_BODIES = "requestBodies";
3030
public static final String REQUEST_BODY_DOCS = "requestBodyDocs";
31+
32+
public static final String RESPONSES = "responses";
3133
public static final String SUPPORTING_FILES = "supportingFiles";
3234
public static final String MODEL_TESTS = "modelTests";
3335
public static final String MODEL_DOCS = "modelDocs";

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

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,13 @@
2323
import java.util.stream.Collectors;
2424

2525
public class CodegenOperation {
26-
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
2726
public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams,
28-
returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap,
29-
isArray, isMultipart,
30-
isResponseBinary = false, isResponseFile = false, hasReference = false, defaultReturnType = false,
27+
subresourceOperation, isMultipart,
3128
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
3229
isRestful, isDeprecated, isCallbackRequest, uniqueItems, hasDefaultResponse = false,
3330
hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined
34-
public CodegenProperty returnProperty;
35-
public String path, operationId, returnType, returnFormat, httpMethod, returnBaseType,
36-
returnContainer, summary, unescapedNotes, notes, baseName;
37-
public CodegenDiscriminator discriminator;
31+
public String path, operationId, httpMethod,
32+
summary, unescapedNotes, notes, baseName;
3833
public List<Map<String, String>> consumes, produces, prioritizedContentTypes;
3934
public List<CodegenServer> servers = new ArrayList<CodegenServer>();
4035
public CodegenParameter bodyParam;
@@ -50,7 +45,11 @@ public class CodegenOperation {
5045
public List<CodegenParameter> optionalParams = new ArrayList<CodegenParameter>();
5146
public List<CodegenSecurity> authMethods;
5247
public Map<String, CodegenTag> tags;
53-
public List<CodegenResponse> responses = new ArrayList<CodegenResponse>();
48+
public TreeMap<String, CodegenResponse> responses = null;
49+
public TreeMap<Integer, CodegenResponse> statusCodeResponses = null;
50+
public TreeMap<Integer, CodegenResponse> wildcardCodeResponses = null;
51+
52+
public TreeMap<String, CodegenResponse> nonDefaultResponses = null;
5453
public CodegenResponse defaultResponse = null;
5554
public List<CodegenCallback> callbacks = new ArrayList<>();
5655
public Set<String> imports = new HashSet<String>();
@@ -167,15 +166,6 @@ public boolean getHasRequiredParams() {
167166
return nonEmpty(requiredParams);
168167
}
169168

170-
/**
171-
* Check if there's at least one response header
172-
*
173-
* @return true if header response exists, false otherwise
174-
*/
175-
public boolean getHasResponseHeaders() {
176-
return nonEmpty(responseHeaders);
177-
}
178-
179169
/**
180170
* Check if there's at least one example parameter
181171
*
@@ -185,21 +175,22 @@ public boolean getHasExamples() {
185175
return nonEmpty(examples);
186176
}
187177

188-
/**
189-
* Check if there's a default response
190-
*
191-
* @return true if responses contain a default response, false otherwise
192-
*/
193-
public boolean getHasDefaultResponse() {
194-
return responses.stream().anyMatch(response -> response.isDefault);
195-
}
196-
197178
public boolean getAllResponsesAreErrors() {
198-
return responses.stream().allMatch(response -> response.is4xx || response.is5xx);
199-
}
200-
201-
public List<CodegenResponse> getNonDefaultResponses() {
202-
return responses.stream().filter(response -> !response.isDefault).collect(Collectors.toList());
179+
if (responses.size() == 1 && defaultResponse != null) {
180+
return false;
181+
}
182+
for (String code: nonDefaultResponses.keySet()) {
183+
String firstNumber = code.substring(0, 1);
184+
switch (firstNumber) {
185+
case "1": case "2": case "3":
186+
return false;
187+
}
188+
}
189+
if (defaultResponse != null) {
190+
// 404 + default, unable to tell if default is a success or an error status code
191+
return false;
192+
}
193+
return true;
203194
}
204195

205196
/**
@@ -314,25 +305,15 @@ private boolean isMemberPath() {
314305
@Override
315306
public String toString() {
316307
final StringBuffer sb = new StringBuffer("CodegenOperation{");
317-
sb.append("responseHeaders=").append(responseHeaders);
318308
sb.append(", hasAuthMethods=").append(hasAuthMethods);
319309
sb.append(", hasConsumes=").append(hasConsumes);
320310
sb.append(", hasProduces=").append(hasProduces);
321311
sb.append(", hasParams=").append(hasParams);
322312
sb.append(", hasOptionalParams=").append(hasOptionalParams);
323313
sb.append(", hasRequiredParams=").append(hasRequiredParams);
324-
sb.append(", returnTypeIsPrimitive=").append(returnTypeIsPrimitive);
325-
sb.append(", returnSimpleType=").append(returnSimpleType);
326314
sb.append(", subresourceOperation=").append(subresourceOperation);
327-
sb.append(", isMap=").append(isMap);
328-
sb.append(", returnProperty=").append(returnProperty);
329-
sb.append(", isArray=").append(isArray);
330315
sb.append(", isMultipart=").append(isMultipart);
331-
sb.append(", isResponseBinary=").append(isResponseBinary);
332-
sb.append(", isResponseFile=").append(isResponseFile);
333-
sb.append(", hasReference=").append(hasReference);
334316
sb.append(", hasDefaultResponse=").append(hasDefaultResponse);
335-
sb.append(", hasErrorResponseObject=").append(hasErrorResponseObject);
336317
sb.append(", isRestfulIndex=").append(isRestfulIndex);
337318
sb.append(", isRestfulShow=").append(isRestfulShow);
338319
sb.append(", isRestfulCreate=").append(isRestfulCreate);
@@ -344,16 +325,12 @@ public String toString() {
344325
sb.append(", uniqueItems='").append(uniqueItems);
345326
sb.append(", path='").append(path).append('\'');
346327
sb.append(", operationId='").append(operationId).append('\'');
347-
sb.append(", returnType='").append(returnType).append('\'');
348328
sb.append(", httpMethod='").append(httpMethod).append('\'');
349-
sb.append(", returnBaseType='").append(returnBaseType).append('\'');
350-
sb.append(", returnContainer='").append(returnContainer).append('\'');
351329
sb.append(", summary='").append(summary).append('\'');
352330
sb.append(", unescapedNotes='").append(unescapedNotes).append('\'');
353331
sb.append(", notes='").append(notes).append('\'');
354332
sb.append(", baseName='").append(baseName).append('\'');
355333
sb.append(", defaultResponse='").append(defaultResponse).append('\'');
356-
sb.append(", discriminator=").append(discriminator);
357334
sb.append(", consumes=").append(consumes);
358335
sb.append(", produces=").append(produces);
359336
sb.append(", prioritizedContentTypes=").append(prioritizedContentTypes);
@@ -371,6 +348,9 @@ public String toString() {
371348
sb.append(", authMethods=").append(authMethods);
372349
sb.append(", tags=").append(tags);
373350
sb.append(", responses=").append(responses);
351+
sb.append(", statusCodeResponses=").append(statusCodeResponses);
352+
sb.append(", wildcardCodeResponses=").append(wildcardCodeResponses);
353+
sb.append(", nonDefaultResponses=").append(nonDefaultResponses);
374354
sb.append(", callbacks=").append(callbacks);
375355
sb.append(", imports=").append(imports);
376356
sb.append(", examples=").append(examples);
@@ -397,17 +377,9 @@ public boolean equals(Object o) {
397377
hasParams == that.hasParams &&
398378
hasOptionalParams == that.hasOptionalParams &&
399379
hasRequiredParams == that.hasRequiredParams &&
400-
returnTypeIsPrimitive == that.returnTypeIsPrimitive &&
401-
returnSimpleType == that.returnSimpleType &&
402380
subresourceOperation == that.subresourceOperation &&
403-
isMap == that.isMap &&
404-
isArray == that.isArray &&
405381
isMultipart == that.isMultipart &&
406-
isResponseBinary == that.isResponseBinary &&
407-
isResponseFile == that.isResponseFile &&
408-
hasReference == that.hasReference &&
409382
hasDefaultResponse == that.hasDefaultResponse &&
410-
hasErrorResponseObject == that.hasErrorResponseObject &&
411383
isRestfulIndex == that.isRestfulIndex &&
412384
isRestfulShow == that.isRestfulShow &&
413385
isRestfulCreate == that.isRestfulCreate &&
@@ -417,20 +389,14 @@ public boolean equals(Object o) {
417389
isDeprecated == that.isDeprecated &&
418390
isCallbackRequest == that.isCallbackRequest &&
419391
uniqueItems == that.uniqueItems &&
420-
Objects.equals(returnProperty, that.returnProperty) &&
421-
Objects.equals(responseHeaders, that.responseHeaders) &&
422392
Objects.equals(path, that.path) &&
423393
Objects.equals(operationId, that.operationId) &&
424-
Objects.equals(returnType, that.returnType) &&
425394
Objects.equals(httpMethod, that.httpMethod) &&
426-
Objects.equals(returnBaseType, that.returnBaseType) &&
427-
Objects.equals(returnContainer, that.returnContainer) &&
428395
Objects.equals(summary, that.summary) &&
429396
Objects.equals(unescapedNotes, that.unescapedNotes) &&
430397
Objects.equals(notes, that.notes) &&
431398
Objects.equals(baseName, that.baseName) &&
432399
Objects.equals(defaultResponse, that.defaultResponse) &&
433-
Objects.equals(discriminator, that.discriminator) &&
434400
Objects.equals(consumes, that.consumes) &&
435401
Objects.equals(produces, that.produces) &&
436402
Objects.equals(prioritizedContentTypes, that.prioritizedContentTypes) &&
@@ -448,6 +414,9 @@ public boolean equals(Object o) {
448414
Objects.equals(authMethods, that.authMethods) &&
449415
Objects.equals(tags, that.tags) &&
450416
Objects.equals(responses, that.responses) &&
417+
Objects.equals(statusCodeResponses, that.statusCodeResponses) &&
418+
Objects.equals(wildcardCodeResponses, that.wildcardCodeResponses) &&
419+
Objects.equals(nonDefaultResponses, that.nonDefaultResponses) &&
451420
Objects.equals(callbacks, that.callbacks) &&
452421
Objects.equals(imports, that.imports) &&
453422
Objects.equals(examples, that.examples) &&
@@ -464,16 +433,17 @@ public boolean equals(Object o) {
464433
@Override
465434
public int hashCode() {
466435

467-
return Objects.hash(responseHeaders, hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams,
468-
hasRequiredParams, returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap,
469-
isArray, isMultipart, isResponseBinary, isResponseFile, hasReference,
436+
return Objects.hash(hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams,
437+
hasRequiredParams, subresourceOperation,
438+
isMultipart,
470439
hasDefaultResponse, isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
471-
isRestful, isDeprecated, isCallbackRequest, uniqueItems, path, operationId, returnType, httpMethod,
472-
returnBaseType, returnContainer, summary, unescapedNotes, notes, baseName, defaultResponse,
473-
discriminator, consumes, produces, prioritizedContentTypes, servers, bodyParam, allParams, bodyParams,
474-
pathParams, queryParams, headerParams, formParams, cookieParams, requiredParams, returnProperty, optionalParams,
440+
isRestful, isDeprecated, isCallbackRequest, uniqueItems, path, operationId, httpMethod,
441+
summary, unescapedNotes, notes, baseName, defaultResponse,
442+
consumes, produces, prioritizedContentTypes, servers, bodyParam, allParams, bodyParams,
443+
pathParams, queryParams, headerParams, formParams, cookieParams, requiredParams, optionalParams,
475444
authMethods, tags, responses, callbacks, imports, examples, requestBodyExamples, externalDocs,
476445
vendorExtensions, nickname, operationIdOriginal, operationIdLowerCase, operationIdCamelCase,
477-
operationIdSnakeCase, hasErrorResponseObject);
446+
operationIdSnakeCase, statusCodeResponses, wildcardCodeResponses,
447+
nonDefaultResponses);
478448
}
479449
}

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

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@
2020
import java.util.*;
2121

2222
public class CodegenResponse {
23-
public final List<CodegenProperty> headers = new ArrayList<CodegenProperty>();
2423
private List<CodegenParameter> responseHeaders = new ArrayList<CodegenParameter>();
25-
public String code;
26-
public boolean is1xx;
27-
public boolean is2xx;
28-
public boolean is3xx;
29-
public boolean is4xx;
30-
public boolean is5xx;
31-
public boolean isDefault;
3224
public String message;
3325
public List<Map<String, Object>> examples;
3426
public boolean hasHeaders;
@@ -41,9 +33,8 @@ public class CodegenResponse {
4133

4234
@Override
4335
public int hashCode() {
44-
return Objects.hash(headers, code, message, examples, hasHeaders,
36+
return Objects.hash(message, examples, hasHeaders,
4537
jsonSchema, vendorExtensions,
46-
is1xx, is2xx, is3xx, is4xx, is5xx, isDefault,
4738
responseHeaders, content,
4839
ref, imports, refModule);
4940
}
@@ -54,24 +45,15 @@ public boolean equals(Object o) {
5445
if (!(o instanceof CodegenResponse)) return false;
5546
CodegenResponse that = (CodegenResponse) o;
5647
return hasHeaders == that.hasHeaders &&
57-
is1xx == that.is1xx &&
58-
is2xx == that.is2xx &&
59-
is3xx == that.is3xx &&
60-
is4xx == that.is4xx &&
61-
is5xx == that.is5xx &&
62-
isDefault == that.isDefault &&
6348
Objects.equals(imports, that.imports) &&
6449
Objects.equals(ref, that.getRef()) &&
6550
Objects.equals(content, that.getContent()) &&
6651
Objects.equals(responseHeaders, that.getResponseHeaders()) &&
67-
Objects.equals(headers, that.headers) &&
68-
Objects.equals(code, that.code) &&
6952
Objects.equals(message, that.message) &&
7053
Objects.equals(examples, that.examples) &&
7154
Objects.equals(jsonSchema, that.jsonSchema) &&
7255
Objects.equals(vendorExtensions, that.vendorExtensions) &&
7356
Objects.equals(refModule, that.getRefModule());
74-
7557
}
7658

7759
public LinkedHashMap<String, CodegenMediaType> getContent() {
@@ -93,14 +75,6 @@ public void setResponseHeaders(List<CodegenParameter> responseHeaders) {
9375
@Override
9476
public String toString() {
9577
final StringBuilder sb = new StringBuilder("CodegenResponse{");
96-
sb.append("headers=").append(headers);
97-
sb.append(", code='").append(code).append('\'');
98-
sb.append(", is1xx='").append(is1xx).append('\'');
99-
sb.append(", is2xx='").append(is2xx).append('\'');
100-
sb.append(", is3xx='").append(is3xx).append('\'');
101-
sb.append(", is4xx='").append(is4xx).append('\'');
102-
sb.append(", is5xx='").append(is5xx).append('\'');
103-
sb.append(", isDefault='").append(isDefault).append('\'');
10478
sb.append(", message='").append(message).append('\'');
10579
sb.append(", examples=").append(examples);
10680
sb.append(", hasHeaders=").append(hasHeaders);
@@ -115,23 +89,6 @@ public String toString() {
11589
return sb.toString();
11690
}
11791

118-
// this is used in templates. Do not remove it.
119-
@SuppressWarnings("unused")
120-
public boolean isWildcard() {
121-
return "0".equals(code) || "default".equals(code);
122-
}
123-
124-
/*
125-
* Boolean value indicating whether the status code is a range
126-
*
127-
* @return True if the status code is a range (e.g. 2XX)
128-
*/
129-
public boolean isRange() {
130-
if (code != null && code.length() == 3 && "XX".equalsIgnoreCase(code.substring(1)))
131-
return true;
132-
return false;
133-
}
134-
13592
public String getRef() { return ref; }
13693

13794
public void setRef(String ref) { this.ref=ref; }

0 commit comments

Comments
 (0)