Revise how to obtain the example value (#326)

* Set the example value from 'Parameter'

* Set the example value from 'Schema'

* Tweak access modifier

* Fix doc comments
This commit is contained in:
Akihito Nakano 2018-05-07 00:38:01 +09:00 committed by William Cheng
parent b1eac05b2b
commit d99f46cff9
3 changed files with 74 additions and 18 deletions

View File

@ -1046,14 +1046,70 @@ public class DefaultCodegen implements CodegenConfig {
* *
* @param codegenParameter Codegen parameter * @param codegenParameter Codegen parameter
*/ */
public void setParameterExampleValue(CodegenParameter codegenParameter) { protected void setParameterExampleValueFromVendorExtensions(CodegenParameter codegenParameter) {
if (codegenParameter.vendorExtensions == null || !codegenParameter.vendorExtensions.containsKey("x-example")) {
return;
}
codegenParameter.example = Json.pretty(codegenParameter.vendorExtensions.get("x-example"));
}
/**
* Return the example value of the parameter.
*
* @param codegenParameter Codegen parameter
* @param parameter parameter
*/
protected void setParameterExampleValue(CodegenParameter codegenParameter, Parameter parameter) {
setParameterExampleValueFromVendorExtensions(codegenParameter);
if (codegenParameter.example != null) {
return;
}
if (parameter.getExample() != null) {
codegenParameter.example = parameter.getExample().toString();
return;
}
setParameterExampleValue(codegenParameter);
}
/**
* Return the example value of the parameter.
*
* @param codegenParameter Codegen parameter
* @param schema schema
*/
protected void setParameterExampleValue(CodegenParameter codegenParameter, Schema schema) {
setParameterExampleValueFromVendorExtensions(codegenParameter);
if (codegenParameter.example != null) {
return;
}
if (schema.getExample() != null) {
codegenParameter.example = schema.getExample().toString();
return;
}
setParameterExampleValue(codegenParameter);
}
/**
* Return the example value of the parameter.
*
* @param codegenParameter Codegen parameter
*/
protected void setParameterExampleValue(CodegenParameter codegenParameter) {
// set the example value // set the example value
// if not specified in x-example, generate a default value // if not specified in x-example, generate a default value
setParameterExampleValueFromVendorExtensions(codegenParameter);
if (codegenParameter.example != null) {
return;
}
// TODO need to revise how to obtain the example value // TODO need to revise how to obtain the example value
if (codegenParameter.vendorExtensions != null && codegenParameter.vendorExtensions.containsKey("x-example")) { if (Boolean.TRUE.equals(codegenParameter.isBoolean)) {
codegenParameter.example = Json.pretty(codegenParameter.vendorExtensions.get("x-example"));
} else if (Boolean.TRUE.equals(codegenParameter.isBoolean)) {
codegenParameter.example = "true"; codegenParameter.example = "true";
} else if (Boolean.TRUE.equals(codegenParameter.isLong)) { } else if (Boolean.TRUE.equals(codegenParameter.isLong)) {
codegenParameter.example = "789"; codegenParameter.example = "789";
@ -2744,7 +2800,7 @@ public class DefaultCodegen implements CodegenConfig {
// set the parameter excample value // set the parameter excample value
// should be overridden by lang codegen // should be overridden by lang codegen
setParameterExampleValue(codegenParameter); setParameterExampleValue(codegenParameter, parameter);
postProcessParameter(codegenParameter); postProcessParameter(codegenParameter);
LOGGER.debug("debugging codegenParameter return: " + codegenParameter); LOGGER.debug("debugging codegenParameter return: " + codegenParameter);
@ -3985,7 +4041,7 @@ public class DefaultCodegen implements CodegenConfig {
/** /**
* returns the list of MIME types the APIs can produce * returns the list of MIME types the APIs can produce
* *
* @param openAPI * @param openAPI OpenAPI
* @param operation Operation * @param operation Operation
* @return a set of MIME types * @return a set of MIME types
*/ */
@ -4211,7 +4267,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterExampleValue(codegenParameter); setParameterExampleValue(codegenParameter, propertySchema);
//TODO collectionFormat for form parameter not yet supported //TODO collectionFormat for form parameter not yet supported
//codegenParameter.collectionFormat = getCollectionFormat(propertySchema); //codegenParameter.collectionFormat = getCollectionFormat(propertySchema);
@ -4369,7 +4425,7 @@ public class DefaultCodegen implements CodegenConfig {
// set the parameter's example value // set the parameter's example value
// should be overridden by lang codegen // should be overridden by lang codegen
setParameterExampleValue(codegenParameter); setParameterExampleValue(codegenParameter, schema);
return codegenParameter; return codegenParameter;
} }

View File

@ -348,7 +348,7 @@ public class ModelUtils {
/** /**
* If a Schema contains a reference to an other Schema with '$ref', returns the referenced Schema or the actual Schema in the other cases. * If a Schema contains a reference to an other Schema with '$ref', returns the referenced Schema or the actual Schema in the other cases.
* @param openAPI * @param openAPI OpenAPI
* @param schema potentially containing a '$ref' * @param schema potentially containing a '$ref'
* @return schema without '$ref' * @return schema without '$ref'
*/ */
@ -374,7 +374,7 @@ public class ModelUtils {
/** /**
* If a RequestBody contains a reference to an other RequestBody with '$ref', returns the referenced RequestBody or the actual RequestBody in the other cases. * If a RequestBody contains a reference to an other RequestBody with '$ref', returns the referenced RequestBody or the actual RequestBody in the other cases.
* @param openAPI * @param openAPI OpenAPI
* @param requestBody potentially containing a '$ref' * @param requestBody potentially containing a '$ref'
* @return requestBody without '$ref' * @return requestBody without '$ref'
*/ */
@ -399,7 +399,7 @@ public class ModelUtils {
/** /**
* If a ApiResponse contains a reference to an other ApiResponse with '$ref', returns the referenced ApiResponse or the actual ApiResponse in the other cases. * If a ApiResponse contains a reference to an other ApiResponse with '$ref', returns the referenced ApiResponse or the actual ApiResponse in the other cases.
* @param openAPI * @param openAPI OpenAPI
* @param apiResponse potentially containing a '$ref' * @param apiResponse potentially containing a '$ref'
* @return apiResponse without '$ref' * @return apiResponse without '$ref'
*/ */

View File

@ -72,9 +72,9 @@ public class URLPathUtils {
/** /**
* Return the port, example value <code>8080</code> * Return the port, example value <code>8080</code>
* @param url * @param url URL
* @param defaultPort if the port is not set * @param defaultPort if the port is not set
* @return * @return port
*/ */
public static String getPort(URL url, int defaultPort) { public static String getPort(URL url, int defaultPort) {
return getPort(url, String.valueOf(defaultPort)); return getPort(url, String.valueOf(defaultPort));
@ -82,9 +82,9 @@ public class URLPathUtils {
/** /**
* Return the port, example value <code>8080</code> * Return the port, example value <code>8080</code>
* @param url * @param url URL
* @param defaultPort if the port is not set * @param defaultPort if the port is not set
* @return * @return port
*/ */
public static String getPort(URL url, String defaultPort) { public static String getPort(URL url, String defaultPort) {
if (url == null || url.getPort() == -1) { if (url == null || url.getPort() == -1) {
@ -96,7 +96,7 @@ public class URLPathUtils {
/** /**
* Return the path, example value <code>/abcdef/xyz</code> * Return the path, example value <code>/abcdef/xyz</code>
* @param url * @param url URL
* @param defaultPath if the path is not empty * @param defaultPath if the path is not empty
* @return path * @return path
*/ */
@ -110,7 +110,7 @@ public class URLPathUtils {
/** /**
* Get the protocol and the host, example value <code>https://www.abcdef.xyz</code> * Get the protocol and the host, example value <code>https://www.abcdef.xyz</code>
* @param url * @param url URL
* @return protocolAndHost * @return protocolAndHost
*/ */
public static String getProtocolAndHost(URL url) { public static String getProtocolAndHost(URL url) {
@ -124,7 +124,7 @@ public class URLPathUtils {
/** /**
* Return the first complete URL from the OpenAPI specification * Return the first complete URL from the OpenAPI specification
* @param openAPI * @param openAPI OpenAPI
* @return host * @return host
*/ */
public static String getHost(OpenAPI openAPI) { public static String getHost(OpenAPI openAPI) {