How to add api doc to generated spring jpa endpoints

81 views Asked by At

I'm trying do document my Spring Data REST API with OpenAPI with spring-doc.
I have my custom endpoints with my custom comments and examples, works fine.

But what about the generated endpoints?
For example for my entity 'host' most endpoints are generated, GET /hosts, PUT /hosts, GET /hosts{id} etc. Only POST and DELETE I have overridden for some special needs, here I added @Operation annotation etc.

I want to add examples for the responses of the GET-requests to illustrate the custom fields in the _links section of hateos result object. (See here: https://github.com/springdoc/springdoc-openapi/issues/237)

But when there is no code, where do I do this?

Thank you.

Juliane

1

There are 1 answers

0
Juliane On

I fixed this for me with a customized OpenApi object.

https://springdoc.org/v1/#how-can-i-customise-the-openapi-object

or for springdoc-openapi v2

https://springdoc.org/#how-can-i-customise-the-openapi-object

    @Bean
public OpenApiCustomiser customize() {
    return openApi -> {
        // addExamplesToGeneratedCode
        openApi.getPaths().forEach((s, pathItem) -> {
        if ("/hosts/{id}".equals(s)) {
            Operation get = pathItem.getGet();
            get.setSummary("Get host");
            ApiResponse response = get.getResponses().get("200");
            MediaType mediaType = response.getContent().get("application/hal+json");
            mediaType.example("{"
                + "  \"id\": 7,"
                + "  \"name\": \"client1\","
                + "  \"role\": \"CLIENT\","
                + "  \"state\": \"INSTALLED\","
                + "  \"username\": \"user\","
                + "  \"_links\": {"
                + "    \"self\": {"
                + "      \"href\": \"http://localhost:8082/hosts/7\""
                + "    },"
                + "    \"host\": {"
                + "      \"href\": \"http://localhost:8082/hosts/7\""
                + "    }"
                + "  }"
                + "}");
        }
    });
}

Maybe there's a better more correct way to do this. But for now, it works for me.