Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtain the example value from examples #419

Merged
merged 3 commits into from Jun 30, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+94 −12
Diff settings

Always

Just for now

@@ -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);
}

@@ -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));

@@ -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();

This comment has been minimized.

Copy link
@ackintosh

ackintosh Jun 30, 2018

Member

Thanks for the follow-up!

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() {
@@ -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,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
@@ -32,9 +46,9 @@ paths:
responses:
'200':
description: successful operation
/example3:
/example3/singular:
get:
operationId: example3Get
operationId: example3GetSingular
parameters:
- name: parameter
in: query
@@ -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:
@@ -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

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.