|
19 | 19 |
|
20 | 20 | import io.swagger.v3.oas.models.OpenAPI; |
21 | 21 | import io.swagger.v3.oas.models.Operation; |
| 22 | +import io.swagger.v3.oas.models.media.Schema; |
22 | 23 | import io.swagger.v3.oas.models.media.StringSchema; |
| 24 | +import java.util.HashSet; |
23 | 25 | import org.openapitools.codegen.CodegenConstants; |
| 26 | +import org.openapitools.codegen.CodegenModelFactory; |
| 27 | +import org.openapitools.codegen.CodegenModelType; |
24 | 28 | import org.openapitools.codegen.CodegenOperation; |
| 29 | +import org.openapitools.codegen.CodegenParameter; |
25 | 30 | import org.openapitools.codegen.TestUtils; |
26 | 31 | import org.openapitools.codegen.languages.PythonClientCodegen; |
| 32 | +import org.openapitools.codegen.utils.ModelUtils; |
27 | 33 | import org.testng.Assert; |
28 | 34 | import org.testng.annotations.Test; |
29 | 35 |
|
@@ -93,12 +99,99 @@ public void testRegularExpressionOpenAPISchemaVersion3() { |
93 | 99 | Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i"); |
94 | 100 | } |
95 | 101 |
|
96 | | - @Test(description = "test single quotes escape") |
97 | | - public void testSingleQuotes() { |
| 102 | + @Test(description = "test default value with single quotes") |
| 103 | + public void testSingleQuotesDefaultValue() { |
98 | 104 | final PythonClientCodegen codegen = new PythonClientCodegen(); |
99 | 105 | StringSchema schema = new StringSchema(); |
100 | 106 | schema.setDefault("Text containing 'single' quote"); |
101 | 107 | String defaultValue = codegen.toDefaultValue(schema); |
102 | | - Assert.assertEquals("'Text containing \'single\' quote'", defaultValue); |
| 108 | + Assert.assertEquals(defaultValue, "\"Text containing 'single' quote\""); |
| 109 | + } |
| 110 | + |
| 111 | + @Test(description = "test example value with single quotes") |
| 112 | + public void testSingleQuotesExampleValue() { |
| 113 | + final PythonClientCodegen codegen = new PythonClientCodegen(); |
| 114 | + StringSchema schema = new StringSchema(); |
| 115 | + schema.setExample("Text containing 'single' quote"); |
| 116 | + String exampleValue = codegen.toExampleValue(schema); |
| 117 | + Assert.assertEquals(exampleValue, "\"Text containing 'single' quote\""); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testFormParameterHasDefaultValue() { |
| 122 | + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml"); |
| 123 | + final PythonClientCodegen codegen = new PythonClientCodegen(); |
| 124 | + codegen.setOpenAPI(openAPI); |
| 125 | + |
| 126 | + Schema requestBodySchema = ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/fake").getGet().getRequestBody()); |
| 127 | + CodegenParameter codegenParameter = codegen.fromFormProperty("enum_form_string", (Schema) requestBodySchema.getProperties().get("enum_form_string"), new HashSet<String>()); |
| 128 | + |
| 129 | + Assert.assertEquals(codegenParameter.defaultValue, "-efg"); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testExample1() { |
| 134 | + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); |
| 135 | + final PythonClientCodegen codegen = new PythonClientCodegen(); |
| 136 | + |
| 137 | + Operation operation = openAPI.getPaths().get("/example1/singular").getGet(); |
| 138 | + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); |
| 139 | + codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); |
| 140 | + |
| 141 | + Assert.assertEquals(codegenParameter.example, "example1 value"); |
| 142 | + |
| 143 | + Operation operation2 = openAPI.getPaths().get("/example1/plural").getGet(); |
| 144 | + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); |
| 145 | + codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0)); |
| 146 | + |
| 147 | + Assert.assertEquals(codegenParameter2.example, "An example1 value"); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testExample2() { |
| 152 | + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); |
| 153 | + final PythonClientCodegen codegen = new PythonClientCodegen(); |
| 154 | + |
| 155 | + Operation operation = openAPI.getPaths().get("/example2/singular").getGet(); |
| 156 | + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); |
| 157 | + codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); |
| 158 | + |
| 159 | + Assert.assertEquals(codegenParameter.example, "example2 value"); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testExample3() { |
| 164 | + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); |
| 165 | + final PythonClientCodegen codegen = new PythonClientCodegen(); |
| 166 | + |
| 167 | + Operation operation = openAPI.getPaths().get("/example3/singular").getGet(); |
| 168 | + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); |
| 169 | + codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); |
| 170 | + |
| 171 | + Assert.assertEquals(codegenParameter.example, "example3: parameter value"); |
| 172 | + |
| 173 | + Operation operation2 = openAPI.getPaths().get("/example3/plural").getGet(); |
| 174 | + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); |
| 175 | + codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0)); |
| 176 | + |
| 177 | + Assert.assertEquals(codegenParameter2.example, "example3: parameter value"); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testExample4() { |
| 182 | + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); |
| 183 | + final PythonClientCodegen codegen = new PythonClientCodegen(); |
| 184 | + |
| 185 | + Operation operation = openAPI.getPaths().get("/example4/singular").getPost(); |
| 186 | + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); |
| 187 | + codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody()); |
| 188 | + |
| 189 | + Assert.assertEquals(codegenParameter.example, "example4 value"); |
| 190 | + |
| 191 | + Operation operation2 = openAPI.getPaths().get("/example4/plural").getPost(); |
| 192 | + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); |
| 193 | + codegen.setParameterExampleValue(codegenParameter2, operation2.getRequestBody()); |
| 194 | + |
| 195 | + Assert.assertEquals(codegenParameter2.example, "An example4 value"); |
103 | 196 | } |
104 | 197 | } |
0 commit comments