Unable to find a model that matches key

Viewed 3404

Upgraded Swagger from 2.9.2 to 3.0.0 based on steps below:

https://springfox.github.io/springfox/docs/snapshot/#migrating-from-existing-2-x-version

In new version, though functionality is working fine, following ERROR is printed in logs when hitting the "/swagger-ui/index.html".

Using springfox-boot-starter 3.0.0. Would appreciate your inputs.

[ReferenceModelSpecificationToPropertyConverter] Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.time', name='LocalDate'}, viewDiscriminator=null, validationGroupDiscriminators=[], isResponse=true}

2 Answers

As per the Springfox documentation

http://springfox.github.io/springfox/docs/current/#answers-to-common-questions-and-problems

The way to correctly map the "Date" and "DateTime" types to their corresponding swagger types:

Substitute "Date" types (java.util.LocalDate, org.joda.time.LocalDate) by java.sql.Date.

Substitute "DateTime" types (java.util.ZonedDateTime, org.joda.time.LocalDateTime, …​) by java.util.Date.

 docket
    .directModelSubstitute(LocalDate.class, java.sql.Date.class)
    .directModelSubstitute(LocalDateTime.class, java.util.Date.class)

Example of Docket bean

@Bean
public Docket api()
{

    Docket docket = new Docket(DocumentationType.SWAGGER_2) //
            .select() //
            .apis(RequestHandlerSelectors.basePackage("com.test")) //
            .paths(PathSelectors.regex("/api/.*")).build() //
            .apiInfo(apiInfo()) //
            .pathMapping("/") //
            .forCodeGeneration(true) //
            .genericModelSubstitutes(ResponseEntity.class) //
            .directModelSubstitute(LocalDate.class, java.sql.Date.class)
            .directModelSubstitute(LocalDateTime.class, java.util.Date.class)
            .useDefaultResponseMessages(false);

    return docket;
}

Not sure if this is your case, but to me this problem arised because I had a JsonSubTyped generic inheritance where one of the children had List<LocalDate> and List<LocalDateTime> as their parameter. The fix was - for me - to include extra rules for the types as follows:

myDocket.alternateTypeRules(
                        AlternateTypeRules.newRule(typeResolver.resolve(List.class, LocalDate.class), typeResolver.resolve(List.class, java.sql.Date.class)), 
                        AlternateTypeRules.newRule(typeResolver.resolve(List.class, LocalDateTime.class), typeResolver.resolve(List.class, java.util.Date.class)))

The replacement classes are from the official site.

Related