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
- Skip events from child application context, fixes #1207 — committed to praveen12bnitt/springfox by praveen12bnitt 8 years ago
- Merge pull request #1233 from praveen12bnitt/bugfix/skip-child-appctx-event Skip events from child application context, fixes #1207 — committed to springfox/springfox by dilipkrish 8 years ago
- Merge branch 'bug/#1207/fixes-context-refresh-event' fixes #1207 — committed to springfox/springfox by dilipkrish 8 years ago
@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.
What i found was, the NPE is coming for
OptimizedModelPropertiesProvider
because theobjectMapper
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 byDocumentationPluginsBootstrapper
. The problem is the main application context initialization is still not complete.RequestMappingHandlerAdapter
is not even created at this time and soObjectMapperConfigurer
does not fireObjectMapperConfigured
. This is the reason whyOptimizedModelPropertiesProvider
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 theContextRefreshedEvent
published by spring-cloud. But i am not sure how to do it.It works fine for me too using 2.4.1-SNAPSHOT.