Search code examples
jodatimeswaggerspringfox

How to replace joda.DateTime with String in springfox?


I'm trying to document my spring (version 3.2.6) app using springfox (version 2.0.2).
My model class has a org.joda.time.DateTime field. When this is rendered in the response model schema it looks something like this:

"dataAsOf": {
  "afterNow": true,
  "beforeNow": true,
  "centuryOfEra": 0,
  "chronology": {
   "zone": {
    "fixed": true
   }
  },
  "dayOfMonth": 0,
  "dayOfWeek": 0,
  "dayOfYear": 0,
  "equalNow": true,
  "era": 0,
  "hourOfDay": 0,
  "millis": 0,
  "millisOfDay": 0,
  "millisOfSecond": 0,
  "minuteOfDay": 0,
  "minuteOfHour": 0,
  "monthOfYear": 0,
  "secondOfDay": 0,
  "secondOfMinute": 0,
  "weekOfWeekyear": 0,
  "weekyear": 0,
  "year": 0,
  "yearOfCentury": 0,
  "yearOfEra": 0,
  "zone": {
   "fixed": true
  }
}


I would really like to get rid of those useless details that my field has.
So far I've tried this in my swagger configuration class:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket restfulApi() {
        String deploymentEnvironment = EnvironmentUtils.getDeployedEnvironment();
        
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                        .enable(isSwaggerEnabled(deploymentEnvironment))
                        .apiInfo(getApiInfo())
                        .directModelSubstitute(DateTime.class, String.class);
        docket = new ApiSelectorBuilder(docket)
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
        
        return docket;
    }

    public ApiInfo getApiInfo() {
        ApiInfo apiInfo = new ApiInfo("API", "RESTful description", "1.0", "terms of service", "email@email.com", "Licence Type", "License URL");
        return apiInfo;
    }

    public boolean isSwaggerEnabled(String deploymentEnvironment) {
        return deploymentEnvironment.equalsIgnoreCase("local") ? true : false;
    }
}


So I've used Docket's directModelSubstitute method to replace the DateTime class with String class, but with no success. The dateAsOf field is still being displayed with all that extra info.
I've also tried using the @ApiOperation(dateType="java.lang.String") on my getter for the dataAsOf field, but it still didn't work.

Can you help me figure out what am I missing?


Solution

  • I've managed to get this to work.
    It appears that first I had to apply theDocket.ignoredParameterTypes(DateTime.class) so that the swagger framework would not generate any documentation for this class and then applyDocket.directModelSubstitute(DateTime.class, String.class) which would actually replace the class.
    Now my model schema is:

    "dataAsOf": "string"