Skip to content

Swagger UI not accessible when FormattingConversionService is a CGLIB proxy #2239

Closed
@wollefitz

Description

@wollefitz

Describe the bug

When formattingConversionService is an instance of an CGLIB proxy, I can't open the Swagger UI. It only displays an error "Internal Server Error /v3/api-docs/api". In the application logs, one can see the following output:

java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "converters" is null
	at org.springdoc.core.providers.WebConversionServiceProvider.getSpringConvertedType(WebConversionServiceProvider.java:118)
	at org.springdoc.core.GenericParameterService.calculateSchema(GenericParameterService.java:360)
	at org.springdoc.core.AbstractRequestService.buildParam(AbstractRequestService.java:530)
	at org.springdoc.core.AbstractRequestService.buildParams(AbstractRequestService.java:490)
	at org.springdoc.core.AbstractRequestService.build(AbstractRequestService.java:312)
	at org.springdoc.api.AbstractOpenApiResource.calculatePath(AbstractOpenApiResource.java:472)
	at org.springdoc.api.AbstractOpenApiResource.calculatePath(AbstractOpenApiResource.java:636)
	at org.springdoc.webmvc.api.OpenApiResource.lambda$calculatePath$11(OpenApiResource.java:219)
	at java.base/java.util.Optional.ifPresent(Optional.java:178)
	at org.springdoc.webmvc.api.OpenApiResource.calculatePath(OpenApiResource.java:200)
	at org.springdoc.webmvc.api.OpenApiResource.lambda$getPaths$2(OpenApiResource.java:170)
	at java.base/java.util.Optional.ifPresent(Optional.java:178)
	at org.springdoc.webmvc.api.OpenApiResource.getPaths(OpenApiResource.java:149)
	at org.springdoc.api.AbstractOpenApiResource.getOpenApi(AbstractOpenApiResource.java:331)
	at org.springdoc.webmvc.api.OpenApiResource.openapiJson(OpenApiResource.java:124)
	at org.springdoc.webmvc.api.OpenApiWebMvcResource.openapiJson(OpenApiWebMvcResource.java:110)
	at org.springdoc.webmvc.api.MultipleOpenApiWebMvcResource.openapiJson(MultipleOpenApiWebMvcResource.java:93)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1072)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	... 111 common frames omitted

The underlying issue is that the converters field of formattingConversionService is accessed via reflection, see

Field convertersField = FieldUtils.getDeclaredField(GenericConversionService.class, CONVERTERS, true);
if (convertersField != null) {
Object converters;
try {
converters = convertersField.get(formattingConversionService);
convertersField = FieldUtils.getDeclaredField(converters.getClass(), CONVERTERS, true);

In Spring, CGLIB proxies do not inherit any instance variables resulting into a null value when accessing the converters field.

This bug was introduced in #1534.

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using?
    2.7.12

  • What modules and versions of springdoc-openapi are you using?
    1.7.0 version of springdoc-openapi-common and springdoc-openapi-ui

  • Provide with a sample code (HelloController) or Test that reproduces the problem
    An application that throws the mentioned error can be found here: https://github.com/wollefitz/springdoc-openapi-demo

Expected behavior

  • A clear and concise description of what you expected to happen.

I'm able to open the Swagger UI at http://server:port/context-path/swagger-ui.html without receiving an error

Screenshots
Screenshot 2023-05-23 at 13 41 43

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions