-
Notifications
You must be signed in to change notification settings - Fork 437
issue-9509 adding support to v3. Escaping for ref defined patterns #1346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
evgeniimv
wants to merge
3
commits into
swagger-api:master
Choose a base branch
from
evgeniimv:issue-9509
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
package io.swagger.codegen.v3.generators; | ||
|
||
import io.swagger.codegen.v3.CodegenArgument; | ||
import io.swagger.codegen.v3.CodegenConstants; | ||
import io.swagger.codegen.v3.CodegenOperation; | ||
import io.swagger.codegen.v3.CodegenParameter; | ||
import io.swagger.codegen.v3.CodegenProperty; | ||
import io.swagger.codegen.v3.CodegenResponse; | ||
import io.swagger.codegen.v3.CodegenType; | ||
import io.swagger.codegen.v3.*; | ||
import io.swagger.codegen.v3.generators.java.*; | ||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.Operation; | ||
|
@@ -23,19 +18,18 @@ | |
import io.swagger.v3.oas.models.responses.ApiResponse; | ||
import io.swagger.v3.parser.OpenAPIV3Parser; | ||
|
||
import java.util.*; | ||
import org.apache.commons.lang3.StringEscapeUtils; | ||
import org.testng.Assert; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
public class DefaultCodegenConfigTest { | ||
|
||
private static final String SSN_ESCAPED_PATTERN = "^\\\\d{3}-\\\\d{2}-\\\\d{4}$"; | ||
|
||
@Test | ||
public void testInitialValues() throws Exception { | ||
final DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
|
@@ -107,7 +101,7 @@ public void testFromOperation_BodyParamsUnique() { | |
PathItem dummyPath = new PathItem() | ||
.post(new Operation()) | ||
.get(new Operation()); | ||
|
||
OpenAPI openAPI = new OpenAPI() | ||
.path("dummy", dummyPath); | ||
|
||
|
@@ -121,7 +115,7 @@ public void testFromOperation_BodyParamsUnique() { | |
operation.setRequestBody(body); | ||
Parameter param = new Parameter().in("query").name("testParameter"); | ||
operation.addParametersItem(param); | ||
|
||
CodegenOperation codegenOperation = codegen.fromOperation("/path", "GET", operation, null, openAPI); | ||
|
||
Assert.assertEquals(true, codegenOperation.allParams.get(0).getVendorExtensions().get("x-has-more")); | ||
|
@@ -137,7 +131,7 @@ public void testFromOperation_BodyParamsUnique() { | |
@Test(dataProvider = "testGetCollectionFormatProvider") | ||
public void testGetCollectionFormat(Parameter.StyleEnum style, Boolean explode, String expectedCollectionFormat) { | ||
final DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
|
||
ArraySchema paramSchema = new ArraySchema() | ||
.items(new IntegerSchema()); | ||
Parameter param = new Parameter() | ||
|
@@ -146,12 +140,12 @@ public void testGetCollectionFormat(Parameter.StyleEnum style, Boolean explode, | |
.schema(paramSchema) | ||
.style(style) | ||
.explode(explode); | ||
|
||
CodegenParameter codegenParameter = codegen.fromParameter(param, new HashSet<>()); | ||
|
||
Assert.assertEquals(codegenParameter.collectionFormat, expectedCollectionFormat); | ||
} | ||
|
||
@DataProvider(name = "testGetCollectionFormatProvider") | ||
public Object[][] provideData_testGetCollectionFormat() { | ||
// See: https://swagger.io/docs/specification/serialization/#query | ||
|
@@ -160,28 +154,28 @@ public Object[][] provideData_testGetCollectionFormat() { | |
{ Parameter.StyleEnum.FORM, null, "multi" }, | ||
{ null, Boolean.TRUE, "multi" }, | ||
{ Parameter.StyleEnum.FORM, Boolean.TRUE, "multi" }, | ||
|
||
{ null, Boolean.FALSE, "csv" }, | ||
{ Parameter.StyleEnum.FORM, Boolean.FALSE, "csv" }, | ||
|
||
{ Parameter.StyleEnum.SPACEDELIMITED, Boolean.TRUE, "multi" }, | ||
{ Parameter.StyleEnum.SPACEDELIMITED, Boolean.FALSE, "space" }, | ||
{ Parameter.StyleEnum.SPACEDELIMITED, null, "multi" }, | ||
|
||
{ Parameter.StyleEnum.PIPEDELIMITED, Boolean.TRUE, "multi" }, | ||
{ Parameter.StyleEnum.PIPEDELIMITED, Boolean.FALSE, "pipe" }, | ||
{ Parameter.StyleEnum.PIPEDELIMITED, null, "multi" }, | ||
}; | ||
} | ||
|
||
/** | ||
* Tests that {@link DefaultCodegenConfig#fromOperation(String, String, Operation, java.util.Map, OpenAPI)} correctly | ||
* resolves the consumes list when the request body is specified via reference rather than inline. | ||
*/ | ||
@Test | ||
public void testRequestBodyRefConsumesList() { | ||
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/3_0_0/requestBodyRefTest.json"); | ||
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
final String path = "/test/requestBodyRefTest"; | ||
final Operation op = openAPI.getPaths().get(path).getPost(); | ||
final CodegenOperation codegenOp = codegen.fromOperation(path, "post", op, openAPI.getComponents().getSchemas(), openAPI); | ||
|
@@ -196,28 +190,28 @@ public void testRequestBodyRefConsumesList() { | |
/** | ||
* Tests when a 'application/x-www-form-urlencoded' request body is marked as required that all form | ||
* params are also marked as required. | ||
* | ||
* | ||
* @see #testOptionalFormParams() | ||
*/ | ||
@Test | ||
public void testRequiredFormParams() { | ||
// Setup | ||
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
|
||
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/3_0_0/requiredFormParamsTest.yaml"); | ||
final String path = "/test_required"; | ||
|
||
final Operation op = openAPI.getPaths().get(path).getPost(); | ||
Assert.assertNotNull(op); | ||
|
||
// Test | ||
final CodegenOperation codegenOp = codegen.fromOperation(path, "post", op, openAPI.getComponents().getSchemas(), openAPI); | ||
|
||
// Verification | ||
List<CodegenParameter> formParams = codegenOp.getFormParams(); | ||
Assert.assertNotNull(formParams); | ||
Assert.assertEquals(formParams.size(), 2); | ||
|
||
for (CodegenParameter formParam : formParams) { | ||
Assert.assertTrue(formParam.getRequired(), "Form param '" + formParam.getParamName() + "' is not required."); | ||
} | ||
|
@@ -233,28 +227,28 @@ public void testRequiredFormParams() { | |
/** | ||
* Tests when a 'application/x-www-form-urlencoded' request body is marked as optional that all form | ||
* params are also marked as optional. | ||
* | ||
* | ||
* @see #testRequiredFormParams() | ||
*/ | ||
@Test | ||
public void testOptionalFormParams() { | ||
// Setup | ||
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig(); | ||
|
||
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/3_0_0/requiredFormParamsTest.yaml"); | ||
final String path = "/test_optional"; | ||
|
||
final Operation op = openAPI.getPaths().get(path).getPost(); | ||
Assert.assertNotNull(op); | ||
|
||
// Test | ||
final CodegenOperation codegenOp = codegen.fromOperation(path, "post", op, openAPI.getComponents().getSchemas(), openAPI); | ||
|
||
// Verification | ||
List<CodegenParameter> formParams = codegenOp.getFormParams(); | ||
Assert.assertNotNull(formParams); | ||
Assert.assertEquals(formParams.size(), 2); | ||
|
||
for (CodegenParameter formParam : formParams) { | ||
Assert.assertFalse(formParam.getRequired(), "Form param '" + formParam.getParamName() + "' is required."); | ||
} | ||
|
@@ -318,6 +312,46 @@ public void testCommonPrefix(List<Object> vars, String expectedPrefix) { | |
Assert.assertEquals(codegen.findCommonPrefixOfVars(vars), expectedPrefix); | ||
} | ||
|
||
@Test | ||
public void verifyProperJavaEscapingForRefSchemaPatterns() { | ||
//given java client codegen | ||
final AbstractJavaCodegen codegen = new JavaClientCodegen(); | ||
|
||
ApiResponse apiResponse = new ApiResponse(); | ||
Header inlineHeader = new Header().description("This is header1").schema(new Schema().type("string").example("header_val")); | ||
apiResponse.addHeaderObject("header1", inlineHeader); | ||
OpenAPI openAPI = new OpenAPI().components(new Components().responses(new HashMap<>())); | ||
openAPI.getComponents().addHeaders("ref-header1", inlineHeader); | ||
Map<String, Schema> allSchemas = new HashMap<>(); | ||
allSchemas.put("Url", buildStringSchemaWithPattern()); | ||
openAPI.getComponents().setSchemas(allSchemas); | ||
codegen.preprocessOpenAPI(openAPI); | ||
|
||
HashMap<String, Schema> properties = new HashMap<>(); | ||
Schema<Object> refSchema = new Schema<>(); | ||
refSchema.set$ref("#/components/schemas/Url"); | ||
properties.put("url", refSchema); | ||
CodegenModel codegenModel = new CodegenModel(); | ||
|
||
//when | ||
codegen.addVars(codegenModel, properties, new ArrayList<>()); | ||
|
||
//then | ||
Assert.assertNotNull(codegenModel); | ||
Assert.assertEquals(codegenModel.vars.size(), 1); | ||
CodegenProperty urlProperty = codegenModel.vars.get(0); | ||
Assert.assertEquals(urlProperty.getName(), "url"); | ||
//verifying java pattern is properly escaped | ||
Assert.assertEquals(urlProperty.getPattern(), SSN_ESCAPED_PATTERN); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so basically we verify that |
||
} | ||
|
||
private Schema buildStringSchemaWithPattern() { | ||
Schema schema = new Schema(); | ||
schema.setType("string"); | ||
// manually apply unescapeJava as if it was parsed from the source file | ||
schema.setPattern(StringEscapeUtils.unescapeJava(SSN_ESCAPED_PATTERN)); | ||
return schema; | ||
} | ||
@DataProvider(name = "testCommonPrefixProvider") | ||
public Object[][] provideData_testCommonPrefix() { | ||
return new Object[][]{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we want to use
toRegularExpression
defined in child classes likeAbstractJavaCodegen
orTypeScriptAxiosClientCodegen
.Thus, we pass it as a
Function<T,T>
(UnaryOperator<String>
) instead of implementing it in OpenAPIUtil