From cbac1579a9a779055dc4c31e9bf5ba5fe41df905 Mon Sep 17 00:00:00 2001 From: "akihito.nakano" Date: Wed, 30 May 2018 19:17:08 +0900 Subject: [PATCH] set example value from parameter --- .../openapitools/codegen/DefaultCodegen.java | 17 +++++++++++++- .../codegen/DefaultCodegenTest.java | 12 ++++++++++ .../src/test/resources/3_0/examples.yaml | 23 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/examples.yaml 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 7f09e70dc659..d3180c5e87ed 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 @@ -1082,6 +1082,21 @@ public void setParameterExampleValue(CodegenParameter codegenParameter) { } + /** + * Return the example value of the parameter. + * + * @param codegenParameter Codegen parameter + * @param parameter Parameter + */ + public void setParameterExampleValue(CodegenParameter codegenParameter, Parameter parameter) { + if (parameter.getExample() != null) { + codegenParameter.example = parameter.getExample().toString(); + return; + } + + setParameterExampleValue(codegenParameter); + } + /** * Return the example value of the property * @@ -2738,7 +2753,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set imports) // set the parameter excample value // should be overridden by lang codegen - setParameterExampleValue(codegenParameter); + setParameterExampleValue(codegenParameter, parameter); postProcessParameter(codegenParameter); LOGGER.debug("debugging codegenParameter return: " + 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 104f7ce5d5a0..ab06fa3f2c6a 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 @@ -214,4 +214,16 @@ public void testGetSchemaTypeWithComposedSchemaWithOneOf() { Assert.assertNotNull(type); } + + @Test + 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(); + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); + + Assert.assertEquals(codegenParameter.example, "example1 value"); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/examples.yaml b/modules/openapi-generator/src/test/resources/3_0/examples.yaml new file mode 100644 index 000000000000..6acca94449e4 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/examples.yaml @@ -0,0 +1,23 @@ +openapi: 3.0.0 +servers: + - url: 'http://petstore.swagger.io/v2' +info: + version: 1.0.0 + title: OpenAPI Petstore + license: + name: Apache-2.0 + url: 'http://www.apache.org/licenses/LICENSE-2.0.html' +paths: + /example1: + get: + operationId: example1Get + parameters: + - name: parameter + in: query + example: 'example1 value' + schema: + type: string + responses: + '200': + description: successful operation +