I am trying to add a default error model to all endpoints in my API, for some error codes.
I found a partial solution by reading the following issues:
- Springfox -> Springdoc: How to expose additional models
- https://github.com/springdoc/springdoc-openapi/issues/381
This is the bean I am creating for that customisation:
@Bean
public OpenApiCustomiser customOpenApiCustomiser() {
return openApi -> {
openApi.getPaths().values().forEach(pathItem -> pathItem.readOperations().forEach(operation -> {
Schema sharedErrorSchema = ModelConverters.getInstance()
.read(Error.class)
.getOrDefault("Error", new Schema());
MediaType sharedMediaType = new MediaType().schema(sharedErrorSchema);
Content sharedContent = new Content()
.addMediaType(APPLICATION_JSON_VALUE, sharedMediaType);
ApiResponses apiResponses = operation.getResponses();
ApiResponse response = new ApiResponse()
.description("Unhandled server error")
.content(sharedContent);
apiResponses.addApiResponse("500", response);
}));
};
}
And my Error class looks something like:
public class Error {
private String message;
private List<ErrorItem> errorItems;
}
The problem is that when I open one of the endpoints definition in swagger-ui, I am getting the following error:
Could not resolve reference: Could not resolve pointer: /components/schemas/ErrorItem does not exist in document
How can I define the schema for ErrorItem, making it available for swagger-ui?
I am using:
- Spring Boot => 2.2.4.Release
- springdoc-openapi-ui => 1.3.0
- springdoc-openapi-security => 1.3.0