micronaut-openapi
micronaut-openapi copied to clipboard
Private fields documented with @Schema are not show in swagger file (using lombok)
Steps to Reproduce
- Start a basic Micronaut project with the openapi feature.
- Create a class with a private field and annotate it with
@Schema
Expected Behaviour
The generated swagger file should show the annotated fields even if they are private.
Actual Behaviour
Private fields are not shown.
We use the @Data
annotation from lombok to automatically generate getters and setters. The private field is defined in code and documented with @Schema
but we cannot annotate the public getters/setters directly since they are generated automatically.
Environment Information
- Windows 10
- Micronaut 2.2.0
- JDK 11
Example Application
https://github.com/TJKeast/micronaut-lombok-issue/tree/master/src
Swagger file output
openapi: 3.0.1
info:
title: demo
version: "0.0"
paths:
/:
get:
tags:
- Test
summary: Retrieves a test
operationId: getAll
parameters: []
responses:
"200":
description: getAll 200 response
content:
application/json:
schema:
$ref: '#/components/schemas/TestModel'
components:
schemas:
TestModel:
type: object
description: A test class
I've encountered a similar issue. I figured out that private fields can be annotated and it works if you have a public
getter created manually. It doesn't work with Lombok-generated getter or when the getter is package-private. An example app:
https://github.com/a-mroz/microservices-example/blob/openapiSchemaIssueExample/recommendations/src/main/java/dev/amroz/microservices/bookshop/recommendations/api/RecommendationResponse.java
My guess is Lombok is not retaining the javadoc in the AST
I'm not sure how javadoc is used here, but it's highly possible.
What's surprising me more is that it doesn't work with package-private getters either.
probably doesn't make any difference since Micronaut processes the source code
Add Schema
into lombok.copyableAnnotations
properties of Lombok configuration and specify @Schema
annotation on private field. Then @Schema
annotation will be copied to the generated getter and setter.
Create lombok.config
file in the root directory of your project (Gradle project, Eclipse project or ...)
lombok.copyableAnnotations += io.swagger.v3.oas.annotations.media.Schema
Then specify @Schema
annotation to the private fields of the class
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AssetDTO {
@Schema(description = "asset ID")
private Integer id;
private AssetState state;
@Schema(description = "token ID at the NFT contract", minimum="0")
private Integer tokenId;
@Schema(description = "asset title")
private String title;
@Schema(description = "the author of the asset")
private String author;
...
}
For more, refer followings