Skip to content

Commit

Permalink
set example value from parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ackintosh committed May 31, 2018
1 parent f042132 commit cbac157
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -2738,7 +2753,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
23 changes: 23 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/examples.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cbac157

Please sign in to comment.