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

v2 adds wildcard responses #151

Merged
merged 9 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Tags|✗|OAS2,OAS3
|ExternalDocs|✗|OAS2,OAS3

### Operation Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
|Responses_HttpStatusCode|✓|OAS2,OAS3
|Responses_RangedResponseCodes|✗|OAS2,OAS3
|Responses_Default|✓|OAS2,OAS3

### Parameter Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
Expand Down
7 changes: 7 additions & 0 deletions docs/generators/jaxrs-jersey.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Tags|✗|OAS2,OAS3
|ExternalDocs|✗|OAS2,OAS3

### Operation Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
|Responses_HttpStatusCode|✓|OAS2,OAS3
|Responses_RangedResponseCodes|✗|OAS2,OAS3
|Responses_Default|✓|OAS2,OAS3

### Parameter Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
Expand Down
7 changes: 7 additions & 0 deletions docs/generators/jmeter.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Tags|✗|OAS2,OAS3
|ExternalDocs|✗|OAS2,OAS3

### Operation Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
|Responses_HttpStatusCode|✓|OAS2,OAS3
|Responses_RangedResponseCodes|✗|OAS2,OAS3
|Responses_Default|✓|OAS2,OAS3

### Parameter Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
Expand Down
7 changes: 7 additions & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Tags|✗|OAS2,OAS3
|ExternalDocs|✗|OAS2,OAS3

### Operation Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
|Responses_HttpStatusCode|✓|OAS2,OAS3
|Responses_RangedResponseCodes|✗|OAS2,OAS3
|Responses_Default|✓|OAS2,OAS3

### Parameter Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
Expand Down
7 changes: 7 additions & 0 deletions docs/generators/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Tags|✓|OAS2,OAS3
|ExternalDocs|✗|OAS2,OAS3

### Operation Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
|Responses_HttpStatusCode|✓|OAS2,OAS3
|Responses_RangedResponseCodes|✓|OAS2,OAS3
|Responses_Default|✓|OAS2,OAS3

### Parameter Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openapijsonschematools.codegen.meta.features.DataTypeFeature;
import org.openapijsonschematools.codegen.meta.features.DocumentationFeature;
import org.openapijsonschematools.codegen.meta.features.GlobalFeature;
import org.openapijsonschematools.codegen.meta.features.OperationFeature;
import org.openapijsonschematools.codegen.meta.features.ParameterFeature;
import org.openapijsonschematools.codegen.meta.features.SchemaFeature;
import org.openapijsonschematools.codegen.meta.features.SecurityFeature;
Expand All @@ -46,6 +47,7 @@ public class FeatureSet {
private EnumSet<SchemaFeature> schemaFeatures;
private EnumSet<ParameterFeature> parameterFeatures;
private EnumSet<SecurityFeature> securityFeatures;
private EnumSet<OperationFeature> operationFeatures;
private EnumSet<WireFormatFeature> wireFormatFeatures;

private FeatureSet(Builder builder) {
Expand All @@ -59,6 +61,7 @@ private FeatureSet(Builder builder) {
parameterFeatures = builder.parameterFeatures;
securityFeatures = builder.securityFeatures;
wireFormatFeatures = builder.wireFormatFeatures;
operationFeatures = builder.operationFeatures;
}
}

Expand All @@ -81,10 +84,19 @@ public static Builder newBuilder(FeatureSet copy) {
builder.parameterFeatures = copy.getParameterFeatures();
builder.securityFeatures = copy.getSecurityFeatures();
builder.wireFormatFeatures = copy.getWireFormatFeatures();
builder.operationFeatures = copy.getOperationFeatures();
}
return builder;
}

public EnumSet<OperationFeature> getOperationFeatures() {
if (operationFeatures != null) {
return EnumSet.copyOf(operationFeatures);
} else {
return EnumSet.noneOf(OperationFeature.class);
}
}

/**
* Returns the set of client modification features supported by the generator.
*
Expand Down Expand Up @@ -354,6 +366,22 @@ public List<FeatureSetFlattened> flatten() {

states.add(state);
});
EnumSet.allOf(OperationFeature.class).forEach(feat -> {
FeatureSetFlattened state = new FeatureSetFlattened();
state.featureCategory = OperationFeature.class.getSimpleName();
state.featureName = feat.name();
state.isSupported = this.operationFeatures.contains(feat);

try {
for (Annotation an : OperationFeature.class.getField(feat.name()).getAnnotations()) {
state.source.add(AnnotationType.fromAnnotation(an.annotationType()));
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}

states.add(state);
});
EnumSet.allOf(WireFormatFeature.class).forEach(feat -> {
FeatureSetFlattened state = new FeatureSetFlattened();
state.featureCategory = WireFormatFeature.class.getSimpleName();
Expand Down Expand Up @@ -389,6 +417,8 @@ public static final class Builder {
private EnumSet<SecurityFeature> securityFeatures;
private EnumSet<WireFormatFeature> wireFormatFeatures;

private EnumSet<OperationFeature> operationFeatures;

private Builder() {
this.clientModificationFeatures = EnumSet.noneOf(ClientModificationFeature.class);
this.dataTypeFeatures = EnumSet.noneOf(DataTypeFeature.class);
Expand All @@ -399,6 +429,26 @@ private Builder() {
this.globalFeatures = EnumSet.noneOf(GlobalFeature.class);
this.componentsFeatures = EnumSet.noneOf(ComponentsFeature.class);
this.wireFormatFeatures = EnumSet.noneOf(WireFormatFeature.class);
this.operationFeatures = EnumSet.noneOf(OperationFeature.class);
}

public Builder operationFeatures(EnumSet<OperationFeature> operationFeatures) {
if (operationFeatures != null) {
this.operationFeatures = operationFeatures;
} else {
this.operationFeatures = EnumSet.noneOf(OperationFeature.class);
}
return this;
}

public Builder includeOperationFeatures(OperationFeature... operationFeature) {
this.operationFeatures.addAll(Arrays.stream(operationFeature).collect(Collectors.toList()));
return this;
}

public Builder excludeOperationFeatures(OperationFeature... operationFeature) {
this.operationFeatures.removeAll(Arrays.stream(operationFeature).collect(Collectors.toList()));
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openapijsonschematools.codegen.meta.features;

import org.openapijsonschematools.codegen.meta.features.annotations.OAS2;
import org.openapijsonschematools.codegen.meta.features.annotations.OAS3;

/**
* Defines Operation features supported in the generated code.
*/
public enum OperationFeature {
/**
* Supports header-based basic http auth.
*/
@OAS2 @OAS3
Responses_HttpStatusCode,

@OAS2 @OAS3
Responses_RangedResponseCodes,

@OAS2 @OAS3
Responses_Default,
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.openapijsonschematools.codegen.meta.features.DataTypeFeature;
import org.openapijsonschematools.codegen.meta.features.DocumentationFeature;
import org.openapijsonschematools.codegen.meta.features.GlobalFeature;
import org.openapijsonschematools.codegen.meta.features.OperationFeature;
import org.openapijsonschematools.codegen.meta.features.ParameterFeature;
import org.openapijsonschematools.codegen.meta.features.SchemaFeature;
import org.openapijsonschematools.codegen.meta.features.SecurityFeature;
Expand Down Expand Up @@ -160,6 +161,9 @@ public class DefaultCodegen implements CodegenConfig {
ParameterFeature.In_Path, ParameterFeature.In_Query, ParameterFeature.In_Header,
ParameterFeature.In_Cookie
)
.includeOperationFeatures(
OperationFeature.Responses_Default, OperationFeature.Responses_HttpStatusCode
)
.includeSecurityFeatures(
SecurityFeature.ApiKey, SecurityFeature.HTTP_Basic, SecurityFeature.HTTP_Bearer,
SecurityFeature.OAuth2_Implicit, SecurityFeature.OAuth2_Password,
Expand Down Expand Up @@ -3379,7 +3383,8 @@ private void updateComponentsFilepath(String[] pathPieces) {
}
} else if (pathPieces[2].equals("responses")) {
// #/components/responses/SuccessWithJsonApiResponse/headers
pathPieces[3] = toResponseModuleName(pathPieces[3], null);
String responseJsonPath = "#/components/responses/" + pathPieces[3];
pathPieces[3] = toResponseModuleName(pathPieces[3], responseJsonPath);

if (pathPieces.length < 6) {
return;
Expand Down Expand Up @@ -3407,6 +3412,7 @@ private void updatePathsFilepath(String[] pathPieces) {
return;
}
// #/paths/somePath
String path = pathPieces[2];
pathPieces[2] = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]), null);
if (pathPieces.length < 4) {
return;
Expand Down Expand Up @@ -3434,8 +3440,8 @@ private void updatePathsFilepath(String[] pathPieces) {
pathPieces[5] = toSecurityRequirementObjectFilename(pathPieces[5], null);
} else if (pathPieces[4].equals("responses")) {
// #/paths/user_login/get/responses/200 -> 200 -> response_200 -> length 6
pathPieces[5] = toResponseModuleName(pathPieces[5], null);

String responseJsonPath = "#/paths/" + path + "/" + pathPieces[3] + "/responses/" + pathPieces[5];
pathPieces[5] = toResponseModuleName(pathPieces[5], responseJsonPath);
if (pathPieces.length < 8) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ private void generatePathItem(List<File> files, CodegenKey pathKey, CodegenPathI
}

if (pathItem.operations != null) {
String testInitFilename = filenameFromRoot(Arrays.asList("test", "test_paths", "test_" + pathKey.snakeCase, "__init__.py"));
generateFile(new HashMap<>(), "__init__.hbs", testInitFilename, files, true, CodegenConstants.API_TESTS);

for (Map.Entry<CodegenKey, CodegenOperation> entry: pathItem.operations.entrySet()) {
CodegenKey httpMethod = entry.getKey();
CodegenOperation operation = entry.getValue();
Expand Down Expand Up @@ -533,9 +536,6 @@ private void generatePathItem(List<File> files, CodegenKey pathKey, CodegenPathI
endpointTestMap.put("packageName", config.packageName());
outputFilename = filenameFromRoot(Arrays.asList("test", "test_paths", "test_" + pathKey.snakeCase, "test_" + httpMethod.original + ".py"));
generateFile(endpointTestMap, templateFile, outputFilename, files, true, CodegenConstants.API_TESTS);

outputFilename = filenameFromRoot(Arrays.asList("test", "test_paths", "test_" + pathKey.snakeCase, "__init__.py"));
generateFile(endpointTestMap, templateFile, outputFilename, files, true, CodegenConstants.API_TESTS);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openapijsonschematools.codegen.CliOption;
import org.openapijsonschematools.codegen.CodegenConstants;
import org.openapijsonschematools.codegen.meta.features.ComponentsFeature;
import org.openapijsonschematools.codegen.meta.features.OperationFeature;
import org.openapijsonschematools.codegen.meta.features.SchemaFeature;
import org.openapijsonschematools.codegen.model.CodegenDiscriminator;
import org.openapijsonschematools.codegen.model.CodegenPatternInfo;
Expand Down Expand Up @@ -226,6 +227,9 @@ public PythonClientCodegen() {
ParameterFeature.Schema,
ParameterFeature.Content
)
.includeOperationFeatures(
OperationFeature.Responses_RangedResponseCodes
)
.excludeParameterFeatures(
ParameterFeature.In_Cookie
)
Expand Down Expand Up @@ -1747,13 +1751,15 @@ public String toApiDocFilename(String name) {

@Override
public String toResponseModuleName(String componentName, String jsonPath) {
String suffix = toModuleFilename(componentName, null);
if (!jsonPath.startsWith("#/components/responses/")) {
return "response_" + componentName.toLowerCase(Locale.ROOT);
}
String suffix = toModuleFilename(componentName, jsonPath);
String spacer = "";
if (!suffix.startsWith("_")) {
spacer = "_";
}
return "response" + spacer + suffix;

}

@Override
Expand Down Expand Up @@ -1911,14 +1917,11 @@ public String getCamelCaseParameter(String name) {
}

public String getCamelCaseResponse(String name) {
try {
Integer.parseInt(name);
// for parameters in path, or an endpoint
if (name.matches("^\\d[X\\d]{2}$")) {
// 200 or 2XX
return "ResponseFor" + name;
} catch (NumberFormatException nfe) {
// for header parameters in responses
return toModelName(name, null);
}
return toModelName(name, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,21 @@ class _ApiResponse(api_client.ApiResponse):
body: schemas.Unset = schemas.unset
{{else}}
{{#if hasContentSchema}}
{{#gt content.size 1}}
body: typing.Union[
{{#each content}}
{{#if this.schema}}
{{#with this.schema}}
{{#each content}}
{{#if this.schema}}
{{#with this.schema}}
{{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}},
{{/with}}
{{else}}
{{/with}}
{{else}}
schemas.Unset,
{{/if}}
{{/each}}
{{/if}}
{{/each}}
]
{{else}}
body: {{#each content}}{{#if this.schema}}{{#with this.schema}}{{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}}{{/with}}{{else}}schemas.Unset{{/if}}{{/each}}
{{/gt}}
{{else}}
body: schemas.Unset = schemas.unset
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#if content}}
{{#each content}}
{{#if schema}}
response_body_schema = {{httpMethod.original}}.response_{{../@key}}.{{responseClassName}}.content["{{{@key.original}}}"].schema
response_body_schema = {{httpMethod.original}}.{{responseModuleName}}.{{responseClassName}}.content["{{{@key.original}}}"].schema
{{/if}}
{{#if this.testCases}}
{{#each testCases}}
Expand Down
Loading