0

We are trying to set up springfox:swagger-ui. However, when creating the restcontroller with a post method using a Value.Immutable interface, the example is showing {} and the model is showing the interface name but nothing else.

I noticed that, when I used the Immutable class as the requestbody, the example is showing all its attributes. I tried adding the subtypes (@ApiModel(subTypes = ImmutableInputLead.class)) and parent configuration (@ApiModel(parent = ImmutableInputLead.class)) on the interface, which didn't change anything

This is our immutable interface:

@Value.Immutable
@ApiModel
@JsonSerialize(as = ImmutableInputLead.class)
@JsonDeserialize(as = ImmutableInputLead.class)
public interface InputLead {
    @Nullable
    @ApiModelProperty(example = "request id")
    String getRequestId();

    @Valid
    Contact getContact();
}

Our mapping:

@ApiOperation(value = "Create a new lead")
@PostMapping(value = "/")
@Validated
public ResponseEntity create(@Valid @RequestBody InputLead inputLead) throws URISyntaxException {
    String leadId = leadService.create(inputLead);
    ResourceRef leadResourceRef = resourceRefFactory.newResourceRef("/api/leads/" + leadId);

    return ResponseEntity.created(new URI(leadResourceRef.getUrl())).build();
}

Our objectmapper:

@Bean
public ObjectMapper objectMapper() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
            .indentOutput(true)
            .dateFormat(new ISO8601DateFormat())
            .serializationInclusion(JsonInclude.Include.NON_EMPTY);

    ObjectMapper objectMapper = builder.build();
    objectMapper.registerModule(new JodaModule());
    objectMapper.registerModule(new GuavaModule());
    objectMapper.registerModule(new MazdaValuesModule());
    objectMapper.registerModule(new DateTimeJacksonModule());
    return objectMapper;
}

Our swagger config:

@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .directModelSubstitute(LocalDateTime.class, String.class)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Lead capturing API").build();
    }

}

I would expect the variables of the InputLead class to be showing in the example.

What am I doing wrong?

Vladas Maier
  • 2,054
  • 2
  • 22
  • 34
Ronny176
  • 265
  • 1
  • 4
  • 13

1 Answers1

0

I believe you've already fixed this, however, I'm going to leave a solution here for future queries.

I'm not sure which swagger version you've used but looks like the swagger@2.x doesn't support @JsonSerializer @JsonDeserializer as you can confirm here.

One way to go is to manually add the alternate type for your immutable interfaces like here

Docket(SWAGGER_2).alternateTypeRules(AlternateTypeRules.newRule(KeyIdentifier.class, ImmutableKeyIdentifier.class))

However, I'm trying to come up with a better solution and will post here if I find it