Skip to content

Commit

Permalink
Revise how to obtain the example value (#326)
Browse files Browse the repository at this point in the history
* Set the example value from 'Parameter'

* Set the example value from 'Schema'

* Tweak access modifier

* Fix doc comments
  • Loading branch information
ackintosh authored and wing328 committed May 6, 2018
1 parent b1eac05 commit d99f46c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1046,14 +1046,70 @@ public String toInstantiationType(Schema schema) {
*
* @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
// 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
if (codegenParameter.vendorExtensions != null && codegenParameter.vendorExtensions.containsKey("x-example")) {
codegenParameter.example = Json.pretty(codegenParameter.vendorExtensions.get("x-example"));
} else if (Boolean.TRUE.equals(codegenParameter.isBoolean)) {
if (Boolean.TRUE.equals(codegenParameter.isBoolean)) {
codegenParameter.example = "true";
} else if (Boolean.TRUE.equals(codegenParameter.isLong)) {
codegenParameter.example = "789";
Expand Down Expand Up @@ -2744,7 +2800,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 @@ -3985,7 +4041,7 @@ private void addProducesInfo(OpenAPI openAPI, ApiResponse inputResponse, Codegen
/**
* returns the list of MIME types the APIs can produce
*
* @param openAPI
* @param openAPI OpenAPI
* @param operation Operation
* @return a set of MIME types
*/
Expand Down Expand Up @@ -4211,7 +4267,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
}

setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterExampleValue(codegenParameter);
setParameterExampleValue(codegenParameter, propertySchema);

//TODO collectionFormat for form parameter not yet supported
//codegenParameter.collectionFormat = getCollectionFormat(propertySchema);
Expand Down Expand Up @@ -4369,7 +4425,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, schema);

return codegenParameter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public static boolean isEmailSchema(Schema schema) {

/**
* 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'
* @return schema without '$ref'
*/
Expand All @@ -374,7 +374,7 @@ public static Schema getSchema(OpenAPI openAPI, String name) {

/**
* 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'
* @return requestBody without '$ref'
*/
Expand All @@ -399,7 +399,7 @@ public static RequestBody getRequestBody(OpenAPI openAPI, String name) {

/**
* 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'
* @return apiResponse without '$ref'
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ public static String getScheme(URL url, CodegenConfig config) {

/**
* Return the port, example value <code>8080</code>
* @param url
* @param url URL
* @param defaultPort if the port is not set
* @return
* @return port
*/
public static String getPort(URL url, int defaultPort) {
return getPort(url, String.valueOf(defaultPort));
}

/**
* Return the port, example value <code>8080</code>
* @param url
* @param url URL
* @param defaultPort if the port is not set
* @return
* @return port
*/
public static String getPort(URL url, String defaultPort) {
if (url == null || url.getPort() == -1) {
Expand All @@ -96,7 +96,7 @@ public static String getPort(URL url, String defaultPort) {

/**
* Return the path, example value <code>/abcdef/xyz</code>
* @param url
* @param url URL
* @param defaultPath if the path is not empty
* @return path
*/
Expand All @@ -110,7 +110,7 @@ public static String getPath(URL url, String defaultPath) {

/**
* Get the protocol and the host, example value <code>https://www.abcdef.xyz</code>
* @param url
* @param url URL
* @return protocolAndHost
*/
public static String getProtocolAndHost(URL url) {
Expand All @@ -124,7 +124,7 @@ public static String getProtocolAndHost(URL url) {

/**
* Return the first complete URL from the OpenAPI specification
* @param openAPI
* @param openAPI OpenAPI
* @return host
*/
public static String getHost(OpenAPI openAPI) {
Expand Down

0 comments on commit d99f46c

Please # to comment.