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

Revise how to obtain the example value #183

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.CookieParameter;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.parameters.Parameter;
Expand Down Expand Up @@ -1083,6 +1080,50 @@ 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;
}

Schema schema = parameter.getSchema();
if (schema != null && schema.getExample() != null) {
codegenParameter.example = schema.getExample().toString();
return;
}

setParameterExampleValue(codegenParameter);
}

/**
* Return the example value of the parameter.
*
* @param codegenParameter Codegen parameter
* @param requestBody Request body
*/
public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) {
Content content = requestBody.getContent();

if (content.size() > 1) {
// @see ModelUtils.getSchemaFromContent()
LOGGER.warn("Multiple MediaTypes found, using only the first one");
}

MediaType mediaType = content.values().iterator().next();
if (mediaType.getExample() != null) {
codegenParameter.example = mediaType.getExample().toString();
return;
}

setParameterExampleValue(codegenParameter);
}

/**
* Return the example value of the property
*
Expand Down Expand Up @@ -2719,7 +2760,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 Expand Up @@ -4328,7 +4369,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc

// set the parameter's example value
// should be overridden by lang codegen
setParameterExampleValue(codegenParameter);
setParameterExampleValue(codegenParameter, body);

return codegenParameter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,54 @@ public void updateCodegenPropertyEnum() {
Assert.assertEquals(testedEnumVar.getOrDefault("isString", ""), false);
}

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

@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();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));

Assert.assertEquals(codegenParameter.example, "example2 value");
}

@Test
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();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));

Assert.assertEquals(codegenParameter.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();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody());

Assert.assertEquals(codegenParameter.example, "example4 value");
}

private CodegenProperty codegenPropertyWithArrayOfIntegerValues() {
CodegenProperty array = new CodegenProperty();
final CodegenProperty items = new CodegenProperty();
Expand Down
63 changes: 63 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,63 @@
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
/example2:
get:
operationId: example2Get
parameters:
- name: parameter
in: query
schema:
type: string
example: 'example2 value'
responses:
'200':
description: successful operation
/example3:
get:
operationId: example3Get
parameters:
- name: parameter
in: query
example: 'example3: parameter value'
schema:
type: string
example: 'example3: schema value'
responses:
'200':
description: successful operation
/example4:
get:
operationId: example4Get
requestBody:
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
name:
type: string
example: 'example4 value'
responses:
'200':
description: successful operation