5

We have a SpringBoot project, and we're using Springfox Swagger for generating the API documentation.

There's a response class that contains:

private Collection<Instant> quartzScheduledDates;

When I run SwaggerUI, I receive this message:

Errors Resolver error at paths./subscriptions/{subscriptionIdStr}.get.responses.200.schema.properties.quartzScheduledDates.items.$ref Could not resolve reference because of: Could not resolve pointer: /definitions/Instant does not exist in document

We're using Springfox Swagger 2.9.2, SpringBoot 2.1.2-RELEASE.

I've also tried using the Docket trick in springfox, as seen in Springfox Documentation:

    docket.directModelSubstitute(Instant.class, java.util.Date.class);

With no success - same error message.

What am I doing wrong?

Vlad Dinulescu
  • 1,173
  • 1
  • 14
  • 24
  • 1
    Your response should not be a problem(I just tested). Please post your endpoint and its configuration or even better create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Januson Mar 08 '19 at 11:06

2 Answers2

9

I was able to replicate this issue. enter image description here

This can be resolved by defining new AlternateTypeRules while creating Docket for your SWAGGER.

Below is the snippet.

Docket docket= new Docket(DocumentationType.SWAGGER_2)
            .alternateTypeRules( AlternateTypeRules.newRule(
                    typeResolver.resolve(Collection.class, Instant.class),
                    typeResolver.resolve(Collection.class, Date.class), Ordered.HIGHEST_PRECEDENCE))
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.test"))
            .paths(PathSelectors.any())
            .build();
NamingException
  • 2,388
  • 1
  • 19
  • 41
  • Thank you :) @VladDinulescu – NamingException Mar 12 '19 at 06:54
  • worked for List in my case: TypeResolver typeResolver = new TypeResolver(); return new Docket(DocumentationType.SWAGGER_2) .alternateTypeRules(AlternateTypeRules.newRule( // my model List typeResolver.resolve(List.class, LocalDate.class), // alternate for JSON definition typeResolver.resolve(List.class, String.class), Ordered.HIGHEST_PRECEDENCE)) ... – phirzel Nov 06 '20 at 20:18
  • typeResolver type is `com.fasterxml.classmate.TypeResolver`. – samabcde Jun 07 '21 at 10:01
1

In my case I had to be really specific. I had:

TreeSet<ZonedDateTime>

Then I had to specifically add:

AlternateTypeRules.newRule(
    typeResolver.resolve(TreeSet.class, ZonedDateTime.class),
    typeResolver.resolve(TreeSet.class, Date.class), Ordered.HIGHEST_PRECEDENCE))

It did NOT work with a parent interface such as: Collection, Set, etc. in the typeResolver. (TypeResolver is @AutoWired by the way if you wonder where it comes from. See docs: https://springfox.github.io/springfox/docs/current/)

marvedly
  • 53
  • 5