diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java index 70c4fe4c3432..575c983cbad6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java @@ -18,6 +18,8 @@ package org.openapitools.codegen.languages; import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.parameters.Parameter; +import io.swagger.v3.oas.models.examples.Example; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; import org.slf4j.Logger; @@ -536,6 +538,34 @@ public void setParameterExampleValue(CodegenParameter p) { p.example = example; } + /** + * Return the example value of the parameter. Overrides the + * setParameterExampleValue(CodegenParameter, Parameter) method in + * DefaultCodegen to always call setParameterExampleValue(CodegenParameter) + * in this class, which adds single quotes around strings from the + * x-example property. + * + * @param codegenParameter Codegen parameter + * @param parameter Parameter + */ + public void setParameterExampleValue(CodegenParameter codegenParameter, Parameter parameter) { + if (parameter.getExample() != null) { + codegenParameter.example = parameter.getExample().toString(); + } else if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) { + Example example = parameter.getExamples().values().iterator().next(); + if (example.getValue() != null) { + codegenParameter.example = example.getValue().toString(); + } + } else { + Schema schema = parameter.getSchema(); + if (schema != null && schema.getExample() != null) { + codegenParameter.example = schema.getExample().toString(); + } + } + + setParameterExampleValue(codegenParameter); + } + public void setGemName(String gemName) { this.gemName = gemName; } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java index 2d89be08cf85..604e601c3254 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java @@ -271,4 +271,33 @@ public void nullableParameterOAS2Test() { // TODO comment out the following until https://github.com/swagger-api/swagger-parser/issues/820 is solved //Assert.assertTrue(status.isNullable); } + + @Test(description = "test example string imported from x-example parameterr (OAS2)") + public void exampleStringFromExampleParameterOAS2Test() { + final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/petstore-nullable.yaml", null, new ParseOptions()).getOpenAPI(); + final RubyClientCodegen codegen = new RubyClientCodegen(); + codegen.setModuleName("OnlinePetstore"); + final String path = "/store/order/{orderId}"; + + final Operation p = openAPI.getPaths().get(path).getDelete(); + final CodegenOperation op = codegen.fromOperation(path, "delete", p, openAPI.getComponents().getSchemas()); + + CodegenParameter pp = op.pathParams.get(0); + Assert.assertEquals(pp.example, "'orderid123'"); + } + + @Test(description = "test example string imported from example in schema (OAS3)") + public void exampleStringFromXExampleParameterOAS3Test() { + final OpenAPI openAPI = new OpenAPIParser() + .readLocation("src/test/resources/3_0/petstore_oas3_test.yaml", null, new ParseOptions()).getOpenAPI(); + final RubyClientCodegen codegen = new RubyClientCodegen(); + codegen.setModuleName("OnlinePetstore"); + final String path = "/store/order/{orderId}"; + + final Operation p = openAPI.getPaths().get(path).getDelete(); + final CodegenOperation op = codegen.fromOperation(path, "delete", p, openAPI.getComponents().getSchemas()); + + CodegenParameter pp = op.pathParams.get(0); + Assert.assertEquals(pp.example, "'orderid123'"); + } } diff --git a/modules/openapi-generator/src/test/resources/2_0/petstore-nullable.yaml b/modules/openapi-generator/src/test/resources/2_0/petstore-nullable.yaml index cff13ed73ce4..f784c3e83f78 100644 --- a/modules/openapi-generator/src/test/resources/2_0/petstore-nullable.yaml +++ b/modules/openapi-generator/src/test/resources/2_0/petstore-nullable.yaml @@ -361,6 +361,7 @@ paths: description: ID of the order that needs to be deleted required: true type: string + x-example: orderid123 responses: '400': description: Invalid ID supplied diff --git a/modules/openapi-generator/src/test/resources/3_0/petstore_oas3_test.yaml b/modules/openapi-generator/src/test/resources/3_0/petstore_oas3_test.yaml index 370ae3ee32f6..b6abd5c6aac8 100644 --- a/modules/openapi-generator/src/test/resources/3_0/petstore_oas3_test.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/petstore_oas3_test.yaml @@ -360,6 +360,7 @@ paths: required: true schema: type: string + example: orderid123 responses: '400': description: Invalid ID supplied