kinbiko
kinbiko

Reputation: 2155

Springfox Swagger generation creates odd schema

I am using Springfox-swagger to generate a Swagger page for my Spring Boot REST service. As a part of one of my POJOs that I serialise to JSON I use:

The issue I'm seeing is that in Swagger, the Model and Model Shema tabs for the response show the implementation details of these fields, namely

LocalDate {
 chronology (IsoChronology, optional),
 dayOfMonth (integer, optional),
 dayOfWeek (string, optional) = ['MONDAY' or 'TUESDAY' or 'WEDNESDAY' or 'THURSDAY' or 'FRIDAY' or 'SATURDAY' or 'SUNDAY'],
 dayOfYear (integer, optional),
 era (Era, optional),
 leapYear (boolean, optional),
 month (string, optional) = ['JANUARY' or 'FEBRUARY' or 'MARCH' or 'APRIL' or 'MAY' or 'JUNE' or 'JULY' or 'AUGUST' or 'SEPTEMBER' or 'OCTOBER' or 'NOVEMBER' or 'DECEMBER'],
 monthValue (integer, optional),
 year (integer, optional)
}
IsoChronology {
  calendarType (string, optional),
  id (string, optional)
}
Era {
  value (integer, optional)
}

and

MonetaryAmount {
  context (MonetaryContext, optional),
  currency (CurrencyUnit, optional),
  factory (MonetaryAmountFactory«MonetaryAmount», optional),
  negative (boolean, optional),
  negativeOrZero (boolean, optional),
  number (NumberValue, optional),
  positive (boolean, optional),
  positiveOrZero (boolean, optional),
  zero (boolean, optional)
}
MonetaryContext {
  (...)
}
CurrencyUnit {
  (...)
}
CurrencyContext {
  (...)
}
MonetaryAmountFactory«MonetaryAmount» {
  (...)
}
NumberValue {
  (...)
}

I am using the Moneta 1.1 implementation of javax.money.MonetaryAmount (JSR 354) if that matters.

Now, this is far to verbose for what I want, so I implemented a Serialiser, and my response (and requests) work as expected. However, the schemas are not reflecting this. Is there a way of configuring the format of the schemas after serialising the requests?

As I realise there are quite a few moving parts to this problem, I've created a proof of concept project to show the problem in its full glory.

Upvotes: 2

Views: 1366

Answers (1)

Dilip Krishnan
Dilip Krishnan

Reputation: 5487

Based on your example looks like you're converting MonetaryAmount to double and LocalDate to String in your serializers.

Because springfox cannot introspect the jackson machinery (at least not yet :-)), we need to tell SpringFox what you're doing in your serializers. To do that, as described in the documentation in your docket you can provide a hint by providing alternate type rules.

docket
      .directModelSubstitute(LocalDate.class, String.class)
      .directModelSubstitute(MonetaryAmount.class, Double.class)

There are three ways to do this, either via directModelSubstitutes for regular types, genericModelSubstitutes for single type argument generic types and alternateTypeRules for more complex generic type use cases.

Upvotes: 3

Related Questions