From ed9ebaa4c8aa5dbb310e7040338b49eb2950adc9 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Fri, 29 Jun 2018 15:52:46 +0200 Subject: [PATCH 1/3] Fix error: "GET operations can not have a requestBody" --- .../java/org/openapitools/codegen/DefaultCodegenTest.java | 2 +- .../openapi-generator/src/test/resources/3_0/examples.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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..6a4f06d88fb0 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 @@ -274,7 +274,7 @@ 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").getPost(); CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody()); 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..ce040ff941db 100644 --- a/modules/openapi-generator/src/test/resources/3_0/examples.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/examples.yaml @@ -46,8 +46,8 @@ paths: '200': description: successful operation /example4: - get: - operationId: example4Get + post: + operationId: example4Post requestBody: content: application/x-www-form-urlencoded: From ed71c363d2947408f7c39ec6b1f331b364972e87 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Fri, 29 Jun 2018 15:56:37 +0200 Subject: [PATCH 2/3] Raname paths --- .../openapitools/codegen/DefaultCodegenTest.java | 8 ++++---- .../src/test/resources/3_0/examples.yaml | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) 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 6a4f06d88fb0..ca88f46ecdda 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,7 +238,7 @@ 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)); @@ -250,7 +250,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,7 +262,7 @@ 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)); @@ -274,7 +274,7 @@ 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").getPost(); + Operation operation = openAPI.getPaths().get("/example4/singular").getPost(); CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody()); 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 ce040ff941db..1aa1b65abcfc 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,9 @@ paths: responses: '200': description: successful operation - /example2: + /example2/singular: get: - operationId: example2Get + operationId: example2GetSingular parameters: - name: parameter in: query @@ -32,9 +32,9 @@ paths: responses: '200': description: successful operation - /example3: + /example3/singular: get: - operationId: example3Get + operationId: example3GetSingular parameters: - name: parameter in: query @@ -45,9 +45,9 @@ paths: responses: '200': description: successful operation - /example4: + /example4/singular: post: - operationId: example4Post + operationId: example4PostSingular requestBody: content: application/x-www-form-urlencoded: From 8ef1f38ef46f2ea81fdb61b89305abd4796d10a0 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Fri, 29 Jun 2018 16:07:52 +0200 Subject: [PATCH 3/3] Add plural form --- .../openapitools/codegen/DefaultCodegen.java | 17 +++++++ .../codegen/DefaultCodegenTest.java | 18 +++++++ .../src/test/resources/3_0/examples.yaml | 47 +++++++++++++++++++ 3 files changed, 82 insertions(+) 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 ca88f46ecdda..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 @@ -243,6 +243,12 @@ public void testExample1() { 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 @@ -267,6 +273,12 @@ public void testExample3() { 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 @@ -279,6 +291,12 @@ public void testExample4() { 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 1aa1b65abcfc..92f6d7b46de4 100644 --- a/modules/openapi-generator/src/test/resources/3_0/examples.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/examples.yaml @@ -20,6 +20,20 @@ paths: responses: '200': description: successful operation + /example1/plural: + get: + 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 @@ -45,6 +59,22 @@ paths: responses: '200': description: successful operation + /example3/plural: + get: + 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 @@ -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