Swagger 3 custom Example Value for 3rd party class

Viewed 610

I am trying to document an API build on Spring Boot 2.5.3 and Swagger 3 (springdoc-openapi version 1.5.10). It's my first contact with Swagger, but until now I managed to rely on the docs to get some things done.

For a specific property of an object (will post details below), I want to provide a hardcoded JSON value for a better idea about what to expect. For my own objects, adding @Schema(example = "SomeStringValue") to object's fields works as expected, meaning that in Swagger-ui the response example value is "SomeStringValue".

Problem is that for a field with a type from 3rd party code (core Spring, in fact), the annotation is ignored and default example is generated, like it wouldn't be annotated at all.

What I have now is below.

Controller:

@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "1. Authentication Controller")
public class AuthenticationController {

    // [...]

    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(description = "Login endpoint")
    public ResponseEntity<AuthenticationC> login(@RequestBody @Valid UserAuthDto userAuthDto, 
        HttpServletRequest request,
        HttpServletResponse response) {

        // [...]
        ResponseEntity<AuthenticationC> authResponse = [...]
        // [...]
        return authResponse;

    }

}

AuthenticationC class:

@Data
@Slf4j
public class AuthenticationC {

    // [...]

    @Schema(example = "Any string I want")
    // This is my own POJO and example works without a problem
    private AuthTokenDetails tokenDetails;

    @Schema(example = "Another string I want")
    // This ignores the example value from @Schema
    private org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse oAuth2AccessTokenResponse;

    // [...]

}

I could workaround that by using a wrapper class, but don't want to modify my objects just to accomodate Swagger.

Another question would be how Jackson is involved in Swagger, as I noticed fields annotated with @JsonIgnore are also ignored in Example Value output.

Thank you.

0 Answers
Related