Skip to content

Commit

Permalink
Obtain the example value from examples (#419)
Browse files Browse the repository at this point in the history
* Fix error: "GET operations can not have a requestBody"
* Add support for "examples" in addition to "support example"
  • Loading branch information
jmini authored Jun 30, 2018
1 parent 0c11718 commit 1f1a47c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,25 @@ 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
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));

Expand All @@ -262,23 +268,35 @@ 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
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() {
Expand Down
63 changes: 55 additions & 8 deletions modules/openapi-generator/src/test/resources/3_0/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -32,9 +46,9 @@ paths:
responses:
'200':
description: successful operation
/example3:
/example3/singular:
get:
operationId: example3Get
operationId: example3GetSingular
parameters:
- name: parameter
in: query
Expand All @@ -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:
Expand All @@ -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

0 comments on commit 1f1a47c

Please sign in to comment.