0

I am using Value.Immutable classes as the rest api request/response. The generated OpenAPI doc does not show the properties of the immutable class. It looks like the OpenAPI generator does not understand these beans as there are no getters and setters in the immutable abstract definition.

How could I properly generate the OpenAPI doc when I use Value.Immutable classes?

Here's a simple example --

The Value.Immutable class:

@Value.Immutable
@JsonSerialize(as = ImmutableCity.class)
@JsonDeserialize(as = ImmutableCity.class)
@Introspected
@Schema(implementation = ImmutableCity.class, name = "City", description = "City model")
@Value.Style(
    passAnnotations = {Schema.class},
    visibility = ImplementationVisibility.PUBLIC)
public abstract class City {

    @JsonProperty("name")
    @NotBlank
    public abstract String name();

    @JsonProperty("description")
    public abstract String description();
}

The controller:

@Controller("/city")
public class CityController {

    /**
     * Persist a city
     * @param city
     * @return the persisted city
     */
    @Post("save")
    @Tag(name = "city")
    public HttpResponse<City> save(
        @Body City city) {
        // persist it...
        return HttpResponse.created(city);
    }
}

The generated OpenAPI schema for City has no properties:

components:
  schemas:
    City:
      type: object
      description: City model

If I change City.java to not use immutables but to have getters/setters, the properties are generated:

components:
  schemas:
    City:
      type: object
      description: City model
      properties:
        name:
          minLength: 1
          type: string
        description:
          type: string
M Perpe
  • 39
  • 1
  • 4
  • 1
    Try configuring Immutables to use a style that generates `get*` and `set*` methods instead of the default. Could be the reason OpenAPI cannot use the instances. http://immutables.github.io/style.html – Henrik Aasted Sørensen Jun 30 '20 at 07:47
  • Tried that and didn't seem to help. – M Perpe Jun 30 '20 at 18:35
  • Changed the immutables to use `get*` and `set*`, and then: (1) If my controller method uses the abstract/interface immutable class, io.micronaut.annotation.processing.visitor.JavaClassElement#getBeanProperties() will skip the methods because they are abstract. But (2) if my controller method uses the generated immutable class, as JavaClassElement#classElement it has an empty member_fields: Scope[] – M Perpe Jun 30 '20 at 18:43

0 Answers0