Fix getReferenced...() methods in ModelUtils (#157)

* New: methods return the element containing the $ref if the referenced
element is not found
* Fix null check in getApiResponse(OpenAPI, String)
* Fix null check in getParameter(OpenAPI, String)
This commit is contained in:
Jérémie Bresson
2018-05-26 15:16:39 +02:00
committed by GitHub
parent 7db0201a89
commit 64f2bea37f
3 changed files with 117 additions and 10 deletions

View File

@@ -423,7 +423,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 if it is found or the actual Schema in the other cases.
* @param openAPI specification being checked
* @param schema potentially containing a '$ref'
* @return schema without '$ref'
@@ -431,7 +431,10 @@ public class ModelUtils {
public static Schema getReferencedSchema(OpenAPI openAPI, Schema schema) {
if (schema != null && StringUtils.isNotEmpty(schema.get$ref())) {
String name = getSimpleRef(schema.get$ref());
return getSchema(openAPI, name);
Schema referencedSchema = getSchema(openAPI, name);
if(referencedSchema != null) {
return referencedSchema;
}
}
return schema;
}
@@ -452,7 +455,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 if it is found or the actual RequestBody in the other cases.
* @param openAPI specification being checked
* @param requestBody potentially containing a '$ref'
* @return requestBody without '$ref'
@@ -460,7 +463,10 @@ public class ModelUtils {
public static RequestBody getReferencedRequestBody(OpenAPI openAPI, RequestBody requestBody) {
if (requestBody != null && StringUtils.isNotEmpty(requestBody.get$ref())) {
String name = getSimpleRef(requestBody.get$ref());
return getRequestBody(openAPI, name);
RequestBody referencedRequestBody = getRequestBody(openAPI, name);
if(referencedRequestBody != null) {
return referencedRequestBody;
}
}
return requestBody;
}
@@ -477,7 +483,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 if it is found or the actual ApiResponse in the other cases.
* @param openAPI specification being checked
* @param apiResponse potentially containing a '$ref'
* @return apiResponse without '$ref'
@@ -485,7 +491,10 @@ public class ModelUtils {
public static ApiResponse getReferencedApiResponse(OpenAPI openAPI, ApiResponse apiResponse) {
if (apiResponse != null && StringUtils.isNotEmpty(apiResponse.get$ref())) {
String name = getSimpleRef(apiResponse.get$ref());
return getApiResponse(openAPI, name);
ApiResponse referencedApiResponse = getApiResponse(openAPI, name);
if(referencedApiResponse != null) {
return referencedApiResponse;
}
}
return apiResponse;
}
@@ -495,14 +504,14 @@ public class ModelUtils {
return null;
}
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getRequestBodies() != null) {
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getResponses() != null) {
return openAPI.getComponents().getResponses().get(name);
}
return null;
}
/**
* If a Parameter contains a reference to an other Parameter with '$ref', returns the referenced Parameter or the actual Parameter in the other cases.
* If a Parameter contains a reference to an other Parameter with '$ref', returns the referenced Parameter if it is found or the actual Parameter in the other cases.
* @param openAPI specification being checked
* @param parameter potentially containing a '$ref'
* @return parameter without '$ref'
@@ -510,7 +519,10 @@ public class ModelUtils {
public static Parameter getReferencedParameter(OpenAPI openAPI, Parameter parameter) {
if (parameter != null && StringUtils.isNotEmpty(parameter.get$ref())) {
String name = getSimpleRef(parameter.get$ref());
return getParameter(openAPI, name);
Parameter referencedParameter = getParameter(openAPI, name);
if(referencedParameter != null) {
return referencedParameter;
}
}
return parameter;
}
@@ -520,7 +532,7 @@ public class ModelUtils {
return null;
}
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getRequestBodies() != null) {
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getParameters() != null) {
return openAPI.getComponents().getParameters().get(name);
}
return null;