forked from loafle/openapi-generator-original
Obtain the example value from examples (#419)
* Fix error: "GET operations can not have a requestBody" * Add support for "examples" in addition to "support example"
This commit is contained in:
parent
0c11718917
commit
1f1a47c57b
@ -24,6 +24,7 @@ import com.samskivert.mustache.Mustache.Compiler;
|
||||
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 class DefaultCodegen implements CodegenConfig {
|
||||
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 class DefaultCodegen implements CodegenConfig {
|
||||
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,11 +238,17 @@ public class DefaultCodegenTest {
|
||||
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
|
||||
@ -250,7 +256,7 @@ public class DefaultCodegenTest {
|
||||
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,11 +268,17 @@ public class DefaultCodegenTest {
|
||||
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
|
||||
@ -274,11 +286,17 @@ public class DefaultCodegenTest {
|
||||
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() {
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user