Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE occurs when outputting an OpenAPI document since 2.5.0 #2561

Merged

Conversation

footaku
Copy link
Contributor

@footaku footaku commented Apr 4, 2024

NPE occurs when outputting an OpenAPI document in a project using springdoc-openapi 2.5.0.
It appears to occur only when certain annotations are included.
Specifically controller class with @Header annotation include @Schema attribute set.

    @Operation(
        // .....
        responses = {
            @ApiResponse(
                responseCode = "201",
                description = "201 (Created)",
                headers = {
                    @Header(
                        name = HttpHeaders.LOCATION,
                        description = "Sample endpoint",
                        schema = @Schema(implementation = URI.class) // include this.
                    )
                })
        }
        // ....
    )
    @GetMapping
    public String foo(){....}

To Reproduce
Steps to reproduce the behavior:

  1. clone https://github.com/footaku/minimum-springdoc-npe
  2. ./gradlew bootRun
  3. curl -v "http://localhost:8080/v3/api-docs"
  • What version of spring-boot you are using?
    • Spring Boot 3.2.4
  • What modules and versions of springdoc-openapi are you using?
    • springdoc-openapi 2.5.0
  • What is the actual and the expected result using OpenAPI Description (yml or json)?
    • JSON
  • Provide with a sample code (HelloController) or Test that reproduces the problem

Expected behavior
NPE does not happen, can get JSON response.

Additional context
Swagger-core have been added getHeader overload methods into AnnotationsUtils.
This change seems to have been made between 2.2.20 and 2.2.21.
swagger-api/swagger-core@v2.2.20...v2.2.21

Around here that is added.
https://github.com/swagger-api/swagger-core/blame/d5c37284f5dbb9b6e65858c25d55c1aa2969f8a2/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java#L1290-L1326

If we do not pass components, it will be thrown NPE here.
https://github.com/swagger-api/swagger-core/blob/d5c37284f5dbb9b6e65858c25d55c1aa2969f8a2/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java#L1387

The swagger-core's AnnotationsUtils has added an overload of the getHeaders method.
A scenario where an NPE occurs if the components is not added as an argument has emerged.
Confirm that setting Header annotations specified in the Schema attribute in ApiResponse's headers attribute does not result in errors.
@bnasslahsen bnasslahsen merged commit 7e78f05 into springdoc:main Apr 9, 2024
@footaku footaku deleted the pass-components-calling-annotation-utils branch April 9, 2024 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants