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

[Spring-java] required readonly attribute generate not null field #5026

Closed
esoni88 opened this issue Jan 17, 2020 · 2 comments · Fixed by #10820
Closed

[Spring-java] required readonly attribute generate not null field #5026

esoni88 opened this issue Jan 17, 2020 · 2 comments · Fixed by #10820

Comments

@esoni88
Copy link

esoni88 commented Jan 17, 2020

Hi All,
regard required, readOnly and writeOnly open api specification report:

If a readOnly or writeOnly property is included in the required list, required affects just the relevant scope – responses only or requests only. That is, read-only required properties apply to responses only, and write-only required properties – to requests only.

so if i declare a field required and read-only means that in the request that field should be empty.

example with this yam:

Poll:
  title: Poll
  type: object
  description: A poll is an election, or a survey of people's opinions to ask inside the platform
  properties:
    id:
      type: integer
      format: int64
      readOnly: true
      description: Unique Identifier of a Poll

openapigenerator generate the class with id not null

@JsonProperty("id")
private Long id;

@NotNull
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

What happen if i run my code and i perform a post with an empty id ? Spring raise bad request but it is not correct. For openapi specification read-only required properties apply to responses only and not for the request.
I think that openapi generator should generate @NotNull only for writeOnly property

@tty0482
Copy link

tty0482 commented Apr 16, 2020

Hi,

I've encountered this same problem. The fix is simple, just change the beanValidation.mustache template slightly - I will attach a pull request in a minute. However, it seems to me that it is likely not only the Spring generator that suffers from this problem, but every Java generator that uses bean validation:

$ find modules/openapi-generator/src/ -name "beanValidation.mustache" -exec grep -l @NotNull {} \;
modules/openapi-generator/src/main/resources/Java/beanValidation.mustache
modules/openapi-generator/src/main/resources/Java/libraries/microprofile/beanValidation.mustache
modules/openapi-generator/src/main/resources/java-pkmst/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaJaxRS/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaJaxRS/cxf/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaJaxRS/cxf-cdi/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaJaxRS/cxf-ext/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaJaxRS/resteasy/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaJaxRS/resteasy/eap/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaJaxRS/spec/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaPlayFramework/beanValidation.mustache
modules/openapi-generator/src/main/resources/JavaSpring/beanValidation.mustache

I didn't look at these files in detail, but it seems likely that all or at least many of these files should receive the same or a similiar fix.

@laDanz
Copy link

laDanz commented Nov 18, 2021

Pretty similar to #9654

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

Successfully merging a pull request may close this issue.

3 participants