diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 43dcc72d7d8d..c4ef8f114af9 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -24,6 +24,7 @@ import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.examples.Example; import io.swagger.v3.oas.models.headers.Header; import io.swagger.v3.oas.models.media.*; import io.swagger.v3.oas.models.parameters.CookieParameter; @@ -1092,6 +1093,14 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete return; } + if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) { + Example example = parameter.getExamples().values().iterator().next(); + if(example.getValue() != null) { + codegenParameter.example = example.getValue().toString(); + return; + } + } + Schema schema = parameter.getSchema(); if (schema != null && schema.getExample() != null) { codegenParameter.example = schema.getExample().toString(); @@ -1121,6 +1130,14 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, RequestB return; } + if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) { + Example example = mediaType.getExamples().values().iterator().next(); + if(example.getValue() != null) { + codegenParameter.example = example.getValue().toString(); + return; + } + } + setParameterExampleValue(codegenParameter); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index bb7c378269df..18ae72beb192 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -238,11 +238,17 @@ public void testExample1() { final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI(); final DefaultCodegen codegen = new DefaultCodegen(); - Operation operation = openAPI.getPaths().get("/example1").getGet(); + Operation operation = openAPI.getPaths().get("/example1/singular").getGet(); CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); Assert.assertEquals(codegenParameter.example, "example1 value"); + + Operation operation2 = openAPI.getPaths().get("/example1/plural").getGet(); + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0)); + + Assert.assertEquals(codegenParameter2.example, "An example1 value"); } @Test @@ -250,7 +256,7 @@ public void testExample2() { final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI(); final DefaultCodegen codegen = new DefaultCodegen(); - Operation operation = openAPI.getPaths().get("/example2").getGet(); + Operation operation = openAPI.getPaths().get("/example2/singular").getGet(); CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); @@ -262,11 +268,17 @@ public void testExample3() { final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI(); final DefaultCodegen codegen = new DefaultCodegen(); - Operation operation = openAPI.getPaths().get("/example3").getGet(); + Operation operation = openAPI.getPaths().get("/example3/singular").getGet(); CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); Assert.assertEquals(codegenParameter.example, "example3: parameter value"); + + Operation operation2 = openAPI.getPaths().get("/example3/plural").getGet(); + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0)); + + Assert.assertEquals(codegenParameter2.example, "example3: parameter value"); } @Test @@ -274,11 +286,17 @@ public void testExample4() { final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI(); final DefaultCodegen codegen = new DefaultCodegen(); - Operation operation = openAPI.getPaths().get("/example4").getGet(); + Operation operation = openAPI.getPaths().get("/example4/singular").getPost(); CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody()); Assert.assertEquals(codegenParameter.example, "example4 value"); + + Operation operation2 = openAPI.getPaths().get("/example4/plural").getPost(); + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter2, operation2.getRequestBody()); + + Assert.assertEquals(codegenParameter2.example, "An example4 value"); } private CodegenProperty codegenPropertyWithArrayOfIntegerValues() { diff --git a/modules/openapi-generator/src/test/resources/3_0/examples.yaml b/modules/openapi-generator/src/test/resources/3_0/examples.yaml index 01c22a5f5be0..92f6d7b46de4 100644 --- a/modules/openapi-generator/src/test/resources/3_0/examples.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/examples.yaml @@ -8,9 +8,9 @@ info: name: Apache-2.0 url: 'http://www.apache.org/licenses/LICENSE-2.0.html' paths: - /example1: + /example1/singular: get: - operationId: example1Get + operationId: example1GetSingular parameters: - name: parameter in: query @@ -20,9 +20,23 @@ paths: responses: '200': description: successful operation - /example2: + /example1/plural: get: - operationId: example2Get + operationId: example1GetPlural + parameters: + - name: parameter + in: query + examples: + AnExample: + value: 'An example1 value' + schema: + type: string + responses: + '200': + description: successful operation + /example2/singular: + get: + operationId: example2GetSingular parameters: - name: parameter in: query @@ -32,9 +46,9 @@ paths: responses: '200': description: successful operation - /example3: + /example3/singular: get: - operationId: example3Get + operationId: example3GetSingular parameters: - name: parameter in: query @@ -45,9 +59,25 @@ paths: responses: '200': description: successful operation - /example4: + /example3/plural: get: - operationId: example4Get + operationId: example3GetSingular + parameters: + - name: parameter + in: query + example: 'example3: parameter value' + examples: + AnExample: + value: 'An example3 value' + schema: + type: string + example: 'example3: schema value' + responses: + '200': + description: successful operation + /example4/singular: + post: + operationId: example4PostSingular requestBody: content: application/x-www-form-urlencoded: @@ -60,4 +90,21 @@ paths: responses: '200': description: successful operation + /example4/plural: + post: + operationId: example4PostSingular + requestBody: + content: + application/x-www-form-urlencoded: + schema: + type: object + properties: + name: + type: string + examples: + AnExample: + value: 'An example4 value' + responses: + '200': + description: successful operation