OpenAPITools / OpenAPITools/openapi-generator

Cannot find symbol for a custom annotation on when using x-field-extra-annotation

Open
#15,432 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I want to add a field annotation to the generated field models via x-field-extra-annotation: '@CustomAnnotation'.
Now my annotation is custom and is in myproject com.project.annotation.CustomAnnotation

I tried to use schemaMappings:

<configuration>
  <schemaMappings>
    <schemaMapping>CustomAnnotation=com.bdl.ihubapi.annotation.CustomAnnotation</schemaMapping>
  </schemaMappings>
</configuration>

But I am having the following error after packagin:

cannot find symbol
[ERROR]   symbol:   class CustomAnnotation
[ERROR]   location: class com.project.model.Test

I also tried to include the following model.mustache:

{{>licenseInfo}}

package {{package}};

{{#imports}}import {{import}};
{{/imports}}
com.project.annotation.CustomAnnotation;

{{#models}}
{{#model}}
/**
 * {{classname}} : {{classVarName}}
 *
{{#description}} * {{{description}}}
 *
{{/description}}{{#vars}}
 * @param {{name}} {{description}}
{{/vars}} */
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
    {{#serializableModel}}
    private static final long serialVersionUID = 1L;
    {{/serializableModel}}
    {{#vars}}
    {{#isEnum}}
    /**
     * Gets or Sets {{{name}}}
     */
    @JsonAdapter({{{name}}}.EnumAdapter.class)
    public enum {{{name}}} {
        {{#allowableValues}}{{#enumVars}}{{{value}}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
        ;

        private String value;

        {{{name}}}(String value) {
            this.value = value;
        }

        public static {{{name}}} fromValue(String text) {
            for ({{{name}}} b : {{{name}}}.values()) {
                if (String.valueOf(b.value).equals(text)) {
                    return b;
                }
            }
            return null;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return String.valueOf(value);
        }

        public static class EnumAdapter extends TypeAdapter<{{{name}}}> {
            @Override
            public void write(final JsonWriter jsonWriter, final {{{name}}} enumeration) throws IOException {
                jsonWriter.value(enumeration.getValue());
            }

            @Override
            public {{{name}}} read(final JsonReader jsonReader) throws IOException {
                String value = jsonReader.nextString();
                return {{{name}}}.fromValue(String.valueOf(value));
            }
        }
    }
    {{/isEnum}}

    {{^isEnum}}
    {{>modelInnerClass}}
    {{/isEnum}}

    {{^isContainer}}
    private {{{datatypeWithEnum}}} {{name}};
    {{/isContainer}}

    {{#isContainer}}
    private {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
    {{/isContainer}}

    /**
     * Get {{{baseName}}}
     * @return {{{baseName}}}
     **/
    @ApiModelProperty({{#required}}required = true, {{/required}}{{^required}}required = false, {{/required}}{{#isReadOnly}}readOnly = true, {{/isReadOnly}}{{#isNullable}}nullable = true, {{/isNullable}}{{#minimum}}minimum = "{{{minimum}}}", {{/minimum}}{{#maximum}}maximum = "{{{maximum}}}", {{/maximum}}{{#minLength}}minLength = {{{minLength}}}, {{/minLength}}{{#maxLength}}maxLength = {{{maxLength}}}, {{/maxLength}}{{#pattern}}pattern = "{{{pattern}}}", {{/pattern}}{{#defaultValue}}"defaultValue" = "{{{defaultValue}}}",{{/defaultValue}}{{#allowableValues}}"allowableValues" = "{{{allowableValues.values}}}",{{/allowableValues}}"example" = "{{{example}}}","value" = "{{{description}}}"){{^required}}
    @JsonProperty("{{{baseName}}}"){{/required}}
    public {{{datatypeWithEnum}}} get{{{getter}}}() {
        return {{name}};
    }

    public void set{{{setter}}}({{{datatypeWithEnum}}} {{{name}}}) {
        this.{{name}} = {{{name}}};
    }
    {{/vars}}

}

{{/model}}
{{/models}}

and add it to the plugin configuration under the path myTemplates/spring/model.mustache:

<templateDirectory>${project.basedir}/myTemplates</templateDirectory>
openapi-generator version

v6.4.0

OpenAPI declaration file content or url
    Test:
      type: object
      properties:
        id:
          type: string
          format: uuid
          readOnly: true
          x-field-extra-annotation: '@CustomAnnotation'
Command line used for generation

mvn clean install

Steps to reproduce

Create a custom annotation interface and add it to the yaml filed x-field-extra-annotation: '@CustomAnnotation'.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CustomAnnotation {
    String id() default "";
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Spring generator's model.mustache override and the Maven plugin schemaMappings configuration, then inspect the generated com.project.model.Test source for imports and the x-field-extra-annotation output. Reproduce with the supplied Test schema and CustomAnnotation, and confirm the generated model compiles when the annotation is available.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.