Excluding child class schema in Swagger documentation

103 Views Asked by At

I am working with Swagger/OpenAPI annotations in Java to document my API, and I have a scenario where I want to exclude schema from a child class in the generated Swagger documentation. Here's a simplified example of my classes:

@Schema(allOf = {
    Student.class,
    Teacher.class
})
public abstract class Person {

  @Schema(description = "Personal Number", example = "00000000-0000")
  private String ssn;

  protected Person() {
  }

  public Person(final String ssn) {
    this.ssn = ssn;
  }

  public void setSsn(final String ssn) {
    this.ssn = ssn;
  }
}
public class Student extends Person {

  @Schema(description = "Student name", example = "Alex")
  private String name;

  private Student() {
  }

  public Student(String ssn, String name) {
    super(ssn);
    this.name = name;
  }

  public String getName() {
    return name;
  }
}
public class Teacher extends Person {

  @Schema(description = "Working place", example = "Gov. School")
  private String school;

  private Teacher() {
  }

  public Teacher(final String ssn, final String school) {
    super(ssn);
    this.school = school;
  }

  public String getSchool() {
    return school;
  }
}

API request endpoint:

@Controller("/person")
public class PersonEndPoint {

  @Post
  public HttpResponse<Void> insertPerson(
      @Body final Person personRequest) {
    return HttpResponse.accepted();
  }

}

This is a micronaut service using swagger 3. I need to hide below schemas from the swagger doc. enter image description here

Despite using @Schema(hidden = true) in the Student class, the child class are still appearing in the Swagger documentation. Is there a way to achieve this exclusion, or is there an alternative approach I can consider?

0

There are 0 best solutions below