springfox: CachingModelPropertiesProvider - NullPointerException

I am using the following spring cloud starter project - spring-cloud-starter-parent:Brixton.M5 I have enabled FeignClient and enabled Swagger2. I had come across an issue when both Swagger and Feign client were enabled together. Based on the inputs given here - https://github.com/springfox/springfox/issues/1074#issuecomment-161551409 , I have adopted the latest springfox version (2.4) and I was able to get past that issue.

The following is an example of an API in my controller:

@ApiOperation(value = "Get University by PK", notes = "Get Universtity based on PK.", response= SampleDTO.class)
@ApiResponses(value = { @ApiResponse(code = 404, message = "University not found") })
@RequestMapping(value = "/university/{pk}", produces = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.GET)
public ResponseEntity<EntityMap> getUniversity(Context context,
           @ApiParam(value = "University PK", required = true) @PathVariable String pk,
           @ApiParam(value = "Template Name", required = false) @RequestParam(required = false) String templateName)
   {
       /*
       *Code comes here
      */
   }

The SampleDTO is annotated with an @ApiModel annoation. When I run my spring boot application, I see the following message in my logs

7:06:40.017 [main] WARN s.d.s.p.CachingModelPropertiesProvider - Exception calculating properties for model(com.manh.cp.fw.sample.university.client.dto.SampleDTO) -> ModelContext{type=com.manh.cp.fw.sample.university.client.dto.SampleDTO, isReturnType=true}. java.lang.NullPointerException

And the Swagger UI has not generated any model schema.

Can you please let me know if this is an issue or if I am missing anything.

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 28 (22 by maintainers)

Commits related to this issue

Most upvoted comments

@dilipkrish here is a sample app which reproduces the issues. https://github.com/praveen12bnitt/springfox-gh1207-issue

Start the app and you will see the below error messages.

2016-03-17 20:17:37.303  WARN 2832 --- [           main] s.d.s.p.CachingModelPropertiesProvider   : Exception calculating properties for model(com.github.prajan.dto.UniversityDTO) -> ModelContext{type=com.github.prajan.dto.UniversityDTO, isReturnType=true}. java.lang.NullPointerException
2016-03-17 20:17:37.311  WARN 2832 --- [           main] s.d.s.p.CachingModelPropertiesProvider   : Exception calculating properties for model(com.github.prajan.dto.UniversityDTO) -> ModelContext{type=com.github.prajan.dto.UniversityDTO, isReturnType=true}. java.lang.NullPointerException
2016-03-17 20:17:37.312  WARN 2832 --- [           main] s.d.s.p.CachingModelPropertiesProvider   : Exception calculating properties for model(com.github.prajan.dto.UniversityDTO) -> ModelContext{type=com.github.prajan.dto.UniversityDTO, isReturnType=false}. java.lang.NullPointerException
2016-03-17 20:17:37.312  WARN 2832 --- [           main] s.d.s.p.CachingModelPropertiesProvider   : Exception calculating properties for model(com.github.prajan.dto.UniversityDTO) -> ModelContext{type=com.github.prajan.dto.UniversityDTO, isReturnType=false}. java.lang.NullPointerException

What i found was, the NPE is coming for OptimizedModelPropertiesProvider because the objectMapper is null.

When a Feign client is added to the application, spring-cloud creates a child application context for feign beans. Check spring cloud’s NamedContextFactory for details on how they create the context. Once this context is refreshed, it signals a ContextRefreshedEvent which is picked up by DocumentationPluginsBootstrapper. The problem is the main application context initialization is still not complete. RequestMappingHandlerAdapter is not even created at this time and so ObjectMapperConfigurer does not fire ObjectMapperConfigured. This is the reason why OptimizedModelPropertiesProvider has a null object mapper.

When RequestMappingHandlerAdapter is created at a later time by the main application context, all the events are fired, but its too late. DocumentationPluginsBootstrapper has already finished its model evaluation.

A possible fix is to wait for the main application’s ContextRefreshedEvent, and skip the ContextRefreshedEvent published by spring-cloud. But i am not sure how to do it.

It works fine for me too using 2.4.1-SNAPSHOT.