Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Annotations on Getters and Setters #125

Closed
gerhardj-b opened this issue Mar 7, 2024 · 11 comments
Closed

Support Annotations on Getters and Setters #125

gerhardj-b opened this issue Mar 7, 2024 · 11 comments

Comments

@gerhardj-b
Copy link

gerhardj-b commented Mar 7, 2024

Hi,

in our case, the models have Annotations - like @NotNull - only on the getters and setters, which are public.
Since some of the code is auto-generated, we also cannot change that behaviour easily.

Is it possible to take such Annotations into account - specifically when determining the required fields of a model?

@kbuntrock
Copy link
Owner

Hello.

Surely, it sounds do-able.

To ease my comprehension / the development : could you provide a sample class representative of what you have on your project?

@gerhardj-b
Copy link
Author

Thanks for your quick response. I created a sample file that results in the said issue. I will paste it here down below. As I explained earlier, those files are auto-generated, namely with the help of openapi-generator-maven-plugin.

This file also shows another issue I found: A List field always leads to uniqueItems being set to true, although this is not neccessarily true in the source.

@JsonPropertyOrder({
  SampleModel.JSON_PROPERTY_NAME,
  SampleModel.JSON_PROPERTY_DESCRIPTION,
  SampleModel.JSON_PROPERTY_TODOS
})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class SampleModel {
  public static final String JSON_PROPERTY_NAME = "name";
  private String name;

  public static final String JSON_PROPERTY_DESCRIPTION = "description";
  private String description;

  public static final String JSON_PROPERTY_TODOS = "todos";
  private List<String> todos;

  public SampleModel() {
  }

  public SampleModel name(String name) {
    
    this.name = name;
    return this;
  }

   /**
   * Get name
   * @return name
  **/
  @jakarta.annotation.Nonnull
  @NotNull

  @JsonProperty(JSON_PROPERTY_NAME)
  @JsonInclude(value = JsonInclude.Include.ALWAYS)

  public String getName() {
    return name;
  }


  @JsonProperty(JSON_PROPERTY_NAME)
  @JsonInclude(value = JsonInclude.Include.ALWAYS)
  public void setName(String name) {
    this.name = name;
  }


  public SampleModel description(String description) {
    
    this.description = description;
    return this;
  }

   /**
   * Get description
   * @return description
  **/
  @jakarta.annotation.Nullable

  @JsonProperty(JSON_PROPERTY_DESCRIPTION)
  @JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)

  public String getDescription() {
    return description;
  }


  @JsonProperty(JSON_PROPERTY_DESCRIPTION)
  @JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
  public void setDescription(String description) {
    this.description = description;
  }


  public SampleModel todos(List<String> todos) {
    
    this.todos = todos;
    return this;
  }

  public SampleModel addTodosItem(String todosItem) {
    if (this.todos == null) {
      this.todos = new ArrayList<>();
    }
    this.todos.add(todosItem);
    return this;
  }

   /**
   * Get todos
   * @return todos
  **/
  @jakarta.annotation.Nullable

  @JsonProperty(JSON_PROPERTY_TODOS)
  @JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)

  public List<String> getTodos() {
    return todos;
  }


  @JsonProperty(JSON_PROPERTY_TODOS)
  @JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
  public void setTodos(List<String> todos) {
    this.todos = todos;
  }

@kbuntrock
Copy link
Owner

Thank you ! I'll be able to have a look on it next week.

@gerhardj
Copy link
Contributor

Hi, I started an attempt to work on this issue. Could you probably give me some early feedback on it or tell me if this is going into the right direction? Then I will ofc give some unit tests a shot.

@kbuntrock
Copy link
Owner

Hello. Thank you @gerhardj for your contribution. :)
I will give feedbacks with pleasure but I'll be able to do so starting from the 02/05. I'm currently abroad without access to a computer to properly review code files.
Thank again for your interest and have a nice day.

Kind regards,
Kevin

gerhardj added a commit to gerhardj/openapi-maven-plugin that referenced this issue Jun 2, 2024
@kbuntrock
Copy link
Owner

@gerhardj Thank you for the PR, I just read it. I need to think a bit about it, mainly the getRelevantAnnotationsForField in the Schema.class. I wonder if there is any way to do this part in a safer way.
Coming back to you soon.

@kbuntrock
Copy link
Owner

This file also shows another issue I found: A List field always leads to uniqueItems being set to true, although this is not neccessarily true in the source.

Reacting also to this part : do you still have the issue with v0.0.19?

@gerhardj-b
Copy link
Author

This file also shows another issue I found: A List field always leads to uniqueItems being set to true, although this is not neccessarily true in the source.

Reacting also to this part : do you still have the issue with v0.0.19?

no, uniqueItems behaves perfectly as expected now, many thanks for that!

gerhardj added a commit to gerhardj/openapi-maven-plugin that referenced this issue Jul 24, 2024
@gerhardj
Copy link
Contributor

Hi, I added a unit test for my changes. Feedback is very welcome, we would be very happy, if this functionality was available in the openapi-maven-plugin, since we really need to migrate from swagger-maven-plugin.

@kbuntrock
Copy link
Owner

Hello @gerhardj.

I merged your PR and released the v0.0.20 a couple of days ago. 😊

@gerhardj-b
Copy link
Author

works perfectly!! many thanks @kbuntrock! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants