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

v2 generate component responses #97

Merged
merged 44 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e60283c
Fixes java tests
spacether Nov 28, 2022
e97a93b
Adds code to generate response modules
spacether Nov 29, 2022
6129a7d
Adds response component
spacether Nov 29, 2022
e94a32b
Refactor, eliminates response code, unused java props and methods rem…
spacether Nov 29, 2022
af8af89
Fixes java ref to vars that are gone, creates template
spacether Nov 29, 2022
152438c
Regen sample, fixes endpoint method type hints
spacether Nov 29, 2022
52024b6
Endpoint doc template update
spacether Nov 29, 2022
ab1df57
Template typo fix
spacether Nov 29, 2022
2801489
Uses integers for status codes and wildcard keys, sample regen
spacether Nov 29, 2022
3173f4b
Sample regen
spacether Nov 29, 2022
44a16f5
Adds printing of response back in
spacether Nov 29, 2022
d060915
nulls response maps initially
spacether Nov 29, 2022
a26872e
Fixes response body type hints in docs
spacether Nov 29, 2022
a85b521
Fixes endpoint docs
spacether Nov 29, 2022
b2ea293
Fixes response code reference in endpoint docs
spacether Nov 29, 2022
310b429
Fixes endpoint test http code, fixes some java tests
spacether Nov 29, 2022
58a0ac4
Adds component responses to readme
spacether Nov 29, 2022
0acaf99
Removes unused java code
spacether Nov 29, 2022
234b53d
Uses refed responses if they exist
spacether Nov 30, 2022
969c9f2
Removes generation of responses when they are referenced
spacether Nov 30, 2022
d4cdbb2
deletes paths and regens it
spacether Nov 30, 2022
aabef36
Template and java update
spacether Nov 30, 2022
104e4a3
Samples regenerated with doc links to component responses
spacether Nov 30, 2022
314c444
Samples regen to include component links
spacether Nov 30, 2022
689d683
Fixes link to component response
spacether Nov 30, 2022
b9d3579
Adds geenration component response inline headers
spacether Nov 30, 2022
754ee85
Sammple updated to include component response with refed content schema
spacether Dec 1, 2022
44ef628
Sample regen and test template tweaked
spacether Dec 1, 2022
5d10040
Fixes component response anchors for headers and body
spacether Dec 1, 2022
289f158
Fixes schema links in component response
spacether Dec 1, 2022
33a912e
Fixes imports for CodegenResponse
spacether Dec 1, 2022
340102b
Adds response with inline content and header definition
spacether Dec 1, 2022
efaa1e1
Sample regenerated
spacether Dec 1, 2022
7cd89d3
Readme links updated
spacether Dec 1, 2022
431d4ab
Simplifies doc link generation
spacether Dec 1, 2022
3c5bdb9
Fixes links
spacether Dec 1, 2022
cd8f107
Fixes module structure of component responses, adds init file
spacether Dec 1, 2022
3bb27e7
Samples regenerated
spacether Dec 1, 2022
2d8a7e6
Fixes tests in DefaultCodegenTest
spacether Dec 1, 2022
449314c
Fixes java tests in JavaModelTest
spacether Dec 1, 2022
3e9ab0c
Disables failing test
spacether Dec 1, 2022
92568a1
Fixes schema links in requestbody docs
spacether Dec 1, 2022
b912e7f
Samples regenerated
spacether Dec 1, 2022
e2b0849
Regen samples with better endpoint test spacing
spacether Dec 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,12 @@ public void testVerboseShort() {
verifyNoMoreInteractions(configurator);
}

/**
* This test ensures that when the
*/
@Test(expectedExceptions = SpecValidationException.class)
public void testNPEWithInvalidSpecFile() {
setupAndRunTest("-i", "src/test/resources/npe-test.yaml", "-g", "java", "-o", "src/main/java", false, null);
}
// /**
// * This test ensures that a NPE is thrown
// * Test stopped passing when swagger-parser setResolve was switched to false
// */
// @Test(expectedExceptions = SpecValidationException.class)
// public void testNPEWithInvalidSpecFile() {
// setupAndRunTest("-i", "src/test/resources/npe-test.yaml", "-g", "java", "-o", "src/main/java", false, null);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
Expand Down Expand Up @@ -88,6 +89,10 @@ public interface CodegenConfig {

String requestBodyFileFolder();

String responseFileFolder(String componentName);

String responseDocFileFolder();

String toApiName(String name);

String toApiVarName(String name);
Expand Down Expand Up @@ -180,6 +185,10 @@ public interface CodegenConfig {

Set<String> pathEndpointResponseHeaderTemplateFiles();

Map<String, String> responseTemplateFiles();

Map<String, String> responseDocTemplateFiles();

Map<String, String> apiTestTemplateFiles();

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

String toRequestBodyDocFilename(String componentName);

String toResponseDocFilename(String componentName);

String toPathFileName(String path);

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

String getBodyParameterName(CodegenOperation co);

CodegenResponse fromResponse(ApiResponse response, String sourceJsonPath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class CodegenConstants {

public static final String REQUEST_BODIES = "requestBodies";
public static final String REQUEST_BODY_DOCS = "requestBodyDocs";

public static final String RESPONSES = "responses";
public static final String SUPPORTING_FILES = "supportingFiles";
public static final String MODEL_TESTS = "modelTests";
public static final String MODEL_DOCS = "modelDocs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@
import java.util.stream.Collectors;

public class CodegenOperation {
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams,
returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap,
isArray, isMultipart,
isResponseBinary = false, isResponseFile = false, hasReference = false, defaultReturnType = false,
subresourceOperation, isMultipart,
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
isRestful, isDeprecated, isCallbackRequest, uniqueItems, hasDefaultResponse = false,
hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined
public CodegenProperty returnProperty;
public String path, operationId, returnType, returnFormat, httpMethod, returnBaseType,
returnContainer, summary, unescapedNotes, notes, baseName;
public CodegenDiscriminator discriminator;
public String path, operationId, httpMethod,
summary, unescapedNotes, notes, baseName;
public List<Map<String, String>> consumes, produces, prioritizedContentTypes;
public List<CodegenServer> servers = new ArrayList<CodegenServer>();
public CodegenParameter bodyParam;
Expand All @@ -50,7 +45,11 @@ public class CodegenOperation {
public List<CodegenParameter> optionalParams = new ArrayList<CodegenParameter>();
public List<CodegenSecurity> authMethods;
public Map<String, CodegenTag> tags;
public List<CodegenResponse> responses = new ArrayList<CodegenResponse>();
public TreeMap<String, CodegenResponse> responses = null;
public TreeMap<Integer, CodegenResponse> statusCodeResponses = null;
public TreeMap<Integer, CodegenResponse> wildcardCodeResponses = null;

public TreeMap<String, CodegenResponse> nonDefaultResponses = null;
public CodegenResponse defaultResponse = null;
public List<CodegenCallback> callbacks = new ArrayList<>();
public Set<String> imports = new HashSet<String>();
Expand Down Expand Up @@ -167,15 +166,6 @@ public boolean getHasRequiredParams() {
return nonEmpty(requiredParams);
}

/**
* Check if there's at least one response header
*
* @return true if header response exists, false otherwise
*/
public boolean getHasResponseHeaders() {
return nonEmpty(responseHeaders);
}

/**
* Check if there's at least one example parameter
*
Expand All @@ -185,21 +175,22 @@ public boolean getHasExamples() {
return nonEmpty(examples);
}

/**
* Check if there's a default response
*
* @return true if responses contain a default response, false otherwise
*/
public boolean getHasDefaultResponse() {
return responses.stream().anyMatch(response -> response.isDefault);
}

public boolean getAllResponsesAreErrors() {
return responses.stream().allMatch(response -> response.is4xx || response.is5xx);
}

public List<CodegenResponse> getNonDefaultResponses() {
return responses.stream().filter(response -> !response.isDefault).collect(Collectors.toList());
if (responses.size() == 1 && defaultResponse != null) {
return false;
}
for (String code: nonDefaultResponses.keySet()) {
String firstNumber = code.substring(0, 1);
switch (firstNumber) {
case "1": case "2": case "3":
return false;
}
}
if (defaultResponse != null) {
// 404 + default, unable to tell if default is a success or an error status code
return false;
}
return true;
}

/**
Expand Down Expand Up @@ -314,25 +305,15 @@ private boolean isMemberPath() {
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("CodegenOperation{");
sb.append("responseHeaders=").append(responseHeaders);
sb.append(", hasAuthMethods=").append(hasAuthMethods);
sb.append(", hasConsumes=").append(hasConsumes);
sb.append(", hasProduces=").append(hasProduces);
sb.append(", hasParams=").append(hasParams);
sb.append(", hasOptionalParams=").append(hasOptionalParams);
sb.append(", hasRequiredParams=").append(hasRequiredParams);
sb.append(", returnTypeIsPrimitive=").append(returnTypeIsPrimitive);
sb.append(", returnSimpleType=").append(returnSimpleType);
sb.append(", subresourceOperation=").append(subresourceOperation);
sb.append(", isMap=").append(isMap);
sb.append(", returnProperty=").append(returnProperty);
sb.append(", isArray=").append(isArray);
sb.append(", isMultipart=").append(isMultipart);
sb.append(", isResponseBinary=").append(isResponseBinary);
sb.append(", isResponseFile=").append(isResponseFile);
sb.append(", hasReference=").append(hasReference);
sb.append(", hasDefaultResponse=").append(hasDefaultResponse);
sb.append(", hasErrorResponseObject=").append(hasErrorResponseObject);
sb.append(", isRestfulIndex=").append(isRestfulIndex);
sb.append(", isRestfulShow=").append(isRestfulShow);
sb.append(", isRestfulCreate=").append(isRestfulCreate);
Expand All @@ -344,16 +325,12 @@ public String toString() {
sb.append(", uniqueItems='").append(uniqueItems);
sb.append(", path='").append(path).append('\'');
sb.append(", operationId='").append(operationId).append('\'');
sb.append(", returnType='").append(returnType).append('\'');
sb.append(", httpMethod='").append(httpMethod).append('\'');
sb.append(", returnBaseType='").append(returnBaseType).append('\'');
sb.append(", returnContainer='").append(returnContainer).append('\'');
sb.append(", summary='").append(summary).append('\'');
sb.append(", unescapedNotes='").append(unescapedNotes).append('\'');
sb.append(", notes='").append(notes).append('\'');
sb.append(", baseName='").append(baseName).append('\'');
sb.append(", defaultResponse='").append(defaultResponse).append('\'');
sb.append(", discriminator=").append(discriminator);
sb.append(", consumes=").append(consumes);
sb.append(", produces=").append(produces);
sb.append(", prioritizedContentTypes=").append(prioritizedContentTypes);
Expand All @@ -371,6 +348,9 @@ public String toString() {
sb.append(", authMethods=").append(authMethods);
sb.append(", tags=").append(tags);
sb.append(", responses=").append(responses);
sb.append(", statusCodeResponses=").append(statusCodeResponses);
sb.append(", wildcardCodeResponses=").append(wildcardCodeResponses);
sb.append(", nonDefaultResponses=").append(nonDefaultResponses);
sb.append(", callbacks=").append(callbacks);
sb.append(", imports=").append(imports);
sb.append(", examples=").append(examples);
Expand All @@ -397,17 +377,9 @@ public boolean equals(Object o) {
hasParams == that.hasParams &&
hasOptionalParams == that.hasOptionalParams &&
hasRequiredParams == that.hasRequiredParams &&
returnTypeIsPrimitive == that.returnTypeIsPrimitive &&
returnSimpleType == that.returnSimpleType &&
subresourceOperation == that.subresourceOperation &&
isMap == that.isMap &&
isArray == that.isArray &&
isMultipart == that.isMultipart &&
isResponseBinary == that.isResponseBinary &&
isResponseFile == that.isResponseFile &&
hasReference == that.hasReference &&
hasDefaultResponse == that.hasDefaultResponse &&
hasErrorResponseObject == that.hasErrorResponseObject &&
isRestfulIndex == that.isRestfulIndex &&
isRestfulShow == that.isRestfulShow &&
isRestfulCreate == that.isRestfulCreate &&
Expand All @@ -417,20 +389,14 @@ public boolean equals(Object o) {
isDeprecated == that.isDeprecated &&
isCallbackRequest == that.isCallbackRequest &&
uniqueItems == that.uniqueItems &&
Objects.equals(returnProperty, that.returnProperty) &&
Objects.equals(responseHeaders, that.responseHeaders) &&
Objects.equals(path, that.path) &&
Objects.equals(operationId, that.operationId) &&
Objects.equals(returnType, that.returnType) &&
Objects.equals(httpMethod, that.httpMethod) &&
Objects.equals(returnBaseType, that.returnBaseType) &&
Objects.equals(returnContainer, that.returnContainer) &&
Objects.equals(summary, that.summary) &&
Objects.equals(unescapedNotes, that.unescapedNotes) &&
Objects.equals(notes, that.notes) &&
Objects.equals(baseName, that.baseName) &&
Objects.equals(defaultResponse, that.defaultResponse) &&
Objects.equals(discriminator, that.discriminator) &&
Objects.equals(consumes, that.consumes) &&
Objects.equals(produces, that.produces) &&
Objects.equals(prioritizedContentTypes, that.prioritizedContentTypes) &&
Expand All @@ -448,6 +414,9 @@ public boolean equals(Object o) {
Objects.equals(authMethods, that.authMethods) &&
Objects.equals(tags, that.tags) &&
Objects.equals(responses, that.responses) &&
Objects.equals(statusCodeResponses, that.statusCodeResponses) &&
Objects.equals(wildcardCodeResponses, that.wildcardCodeResponses) &&
Objects.equals(nonDefaultResponses, that.nonDefaultResponses) &&
Objects.equals(callbacks, that.callbacks) &&
Objects.equals(imports, that.imports) &&
Objects.equals(examples, that.examples) &&
Expand All @@ -464,16 +433,17 @@ public boolean equals(Object o) {
@Override
public int hashCode() {

return Objects.hash(responseHeaders, hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams,
hasRequiredParams, returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap,
isArray, isMultipart, isResponseBinary, isResponseFile, hasReference,
return Objects.hash(hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams,
hasRequiredParams, subresourceOperation,
isMultipart,
hasDefaultResponse, isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
isRestful, isDeprecated, isCallbackRequest, uniqueItems, path, operationId, returnType, httpMethod,
returnBaseType, returnContainer, summary, unescapedNotes, notes, baseName, defaultResponse,
discriminator, consumes, produces, prioritizedContentTypes, servers, bodyParam, allParams, bodyParams,
pathParams, queryParams, headerParams, formParams, cookieParams, requiredParams, returnProperty, optionalParams,
isRestful, isDeprecated, isCallbackRequest, uniqueItems, path, operationId, httpMethod,
summary, unescapedNotes, notes, baseName, defaultResponse,
consumes, produces, prioritizedContentTypes, servers, bodyParam, allParams, bodyParams,
pathParams, queryParams, headerParams, formParams, cookieParams, requiredParams, optionalParams,
authMethods, tags, responses, callbacks, imports, examples, requestBodyExamples, externalDocs,
vendorExtensions, nickname, operationIdOriginal, operationIdLowerCase, operationIdCamelCase,
operationIdSnakeCase, hasErrorResponseObject);
operationIdSnakeCase, statusCodeResponses, wildcardCodeResponses,
nonDefaultResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@
import java.util.*;

public class CodegenResponse {
public final List<CodegenProperty> headers = new ArrayList<CodegenProperty>();
private List<CodegenParameter> responseHeaders = new ArrayList<CodegenParameter>();
public String code;
public boolean is1xx;
public boolean is2xx;
public boolean is3xx;
public boolean is4xx;
public boolean is5xx;
public boolean isDefault;
public String message;
public List<Map<String, Object>> examples;
public boolean hasHeaders;
Expand All @@ -41,9 +33,8 @@ public class CodegenResponse {

@Override
public int hashCode() {
return Objects.hash(headers, code, message, examples, hasHeaders,
return Objects.hash(message, examples, hasHeaders,
jsonSchema, vendorExtensions,
is1xx, is2xx, is3xx, is4xx, is5xx, isDefault,
responseHeaders, content,
ref, imports, refModule);
}
Expand All @@ -54,24 +45,15 @@ public boolean equals(Object o) {
if (!(o instanceof CodegenResponse)) return false;
CodegenResponse that = (CodegenResponse) o;
return hasHeaders == that.hasHeaders &&
is1xx == that.is1xx &&
is2xx == that.is2xx &&
is3xx == that.is3xx &&
is4xx == that.is4xx &&
is5xx == that.is5xx &&
isDefault == that.isDefault &&
Objects.equals(imports, that.imports) &&
Objects.equals(ref, that.getRef()) &&
Objects.equals(content, that.getContent()) &&
Objects.equals(responseHeaders, that.getResponseHeaders()) &&
Objects.equals(headers, that.headers) &&
Objects.equals(code, that.code) &&
Objects.equals(message, that.message) &&
Objects.equals(examples, that.examples) &&
Objects.equals(jsonSchema, that.jsonSchema) &&
Objects.equals(vendorExtensions, that.vendorExtensions) &&
Objects.equals(refModule, that.getRefModule());

}

public LinkedHashMap<String, CodegenMediaType> getContent() {
Expand All @@ -93,14 +75,6 @@ public void setResponseHeaders(List<CodegenParameter> responseHeaders) {
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenResponse{");
sb.append("headers=").append(headers);
sb.append(", code='").append(code).append('\'');
sb.append(", is1xx='").append(is1xx).append('\'');
sb.append(", is2xx='").append(is2xx).append('\'');
sb.append(", is3xx='").append(is3xx).append('\'');
sb.append(", is4xx='").append(is4xx).append('\'');
sb.append(", is5xx='").append(is5xx).append('\'');
sb.append(", isDefault='").append(isDefault).append('\'');
sb.append(", message='").append(message).append('\'');
sb.append(", examples=").append(examples);
sb.append(", hasHeaders=").append(hasHeaders);
Expand All @@ -115,23 +89,6 @@ public String toString() {
return sb.toString();
}

// this is used in templates. Do not remove it.
@SuppressWarnings("unused")
public boolean isWildcard() {
return "0".equals(code) || "default".equals(code);
}

/*
* Boolean value indicating whether the status code is a range
*
* @return True if the status code is a range (e.g. 2XX)
*/
public boolean isRange() {
if (code != null && code.length() == 3 && "XX".equalsIgnoreCase(code.substring(1)))
return true;
return false;
}

public String getRef() { return ref; }

public void setRef(String ref) { this.ref=ref; }
Expand Down
Loading