LaVOZs

The World’s Largest Online Community for Developers

'; java - How to get micronaut generate OpenAPI schema properties for Value.Immutable classes? - LavOzs.Com

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
Related
How do I test a private function or a class that has private methods, fields or inner classes?
How to generate a random alpha-numeric string?
How do I generate random integers within a specific range in Java?
How can I generate an MD5 hash?
How to create a generic array in Java?
How to get an enum value from a string value in Java?
Get generic type of class at runtime
How do I get a class instance of generic type T?
Generate a property as schema definition in OpenApi 3.0