I'm trying to define annotations on shared schemas in order to only have to define said annotations once. When I define x-extra-annotation directly on a property in a schema the generator places the annotation above the getter as expected, but when I extract that property to be a seperate schema, and define the annotation there, the generated model does not have the additional annotation. I imagine I'm doing something wrong, so I'd like to know whats the right way to go about adding annotations.

Concretely, when I generate using the following schema:

components:
  schemas:
    Person:
      type: object
      required:
        - firstname
        - surname
      properties:
        firstname:
          type: string
        surname:
          type: string
          x-extra-annotation: "@SurnameAnnotation"

A model is generated that looks something like:

public class Person {

  private String firstname;

  private String surname;

  // ... omitted for brevity 
  
  /**
   * Get surname
   * @return surname
  */
  @SurnameAnnotation
  @NotNull 
  @Schema(name = "surname", requiredMode = Schema.RequiredMode.REQUIRED)
  @JsonProperty("surname")
  public String getSurname() {
    return surname;
  }

  // ... omitted for brevity 
}

However, when I extract the surname as a separate type in the yaml..:


components:
  schemas:
    Person:
      type: object
      required:
        - firstname
        - surname
      properties:
        firstname:
          type: string
        surname:
          type: surname
    surname:
      type: string
      x-extra-annotation: "@SurnameAnnotation"

The generated model does not include the extra annotation on the getter:

public class Person {

  private String firstname;

  private String surname;

  // ... omitted for brevity 
  
  /**
   * Get surname
   * @return surname
  */
  @NotNull 
  @Schema(name = "surname", requiredMode = Schema.RequiredMode.REQUIRED)
  @JsonProperty("surname")
  public String getSurname() {
    return surname;
  }

  // ... omitted for brevity 
}

I'm using the following plugin config, in case it matters:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>7.4.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>
                    ${project.basedir}/src/main/resources/openapi/helloworld.yml
                </inputSpec>
                <templateDirectory>
                    ${project.basedir}/src/main/resources/openapi/templates/
                </templateDirectory>
                <generatorName>spring</generatorName>
                <apiPackage>nl.myorg.poc</apiPackage>
                <modelPackage>nl.myorg.poc.models</modelPackage>
                <supportingFilesToGenerate></supportingFilesToGenerate>
                <configOptions>
                    <skipDefaultInterface>true</skipDefaultInterface>
                    <interfaceOnly>true</interfaceOnly>
                    <useSpringBoot3>true</useSpringBoot3>
                    <useTags>true</useTags>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

I've also tried the vendor extension for field annotation, but that also doesn't work on a separate schema.

0

There are 0 best solutions below