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

How to set optional and mandatory fields on openapi class schemas? #229

Closed
franck-malka opened this issue Sep 4, 2024 · 7 comments
Closed
Labels
question Further information is requested stale

Comments

@franck-malka
Copy link

How to set optional and mandatory fields on openapi class schemas?

@dzikoysk dzikoysk added the question Further information is requested label Sep 4, 2024
@dzikoysk
Copy link
Member

dzikoysk commented Sep 4, 2024

By default, we're marking nullable fields as optional fields and non-nullalbe fields as required (based on Kotlin return types or @Nullable/@NotNull annotations in Java). If you don't like this behavior, you can put @JsonSchema annotation on a particular type to disable it and use @OpenApiRequired whenever needed:

@JsonSchema(requireNonNulls = false)
data class Entity(
    val wontBeRequired: String
    @OpenApiRequired
    val willBeRequired: String
)

@franck-malka
Copy link
Author

@nullable and @NotNull is fine but it doesn't seems to work for me
Let me try the other method

@franck-malka
Copy link
Author

for example

@jsonschema(requireNonNulls = true)
public class measurementRequest {
@NotNull messageType messageType; //9.2.3
@NotNull long nrppaTransactionId; //9.2.4

fist field is still marked as optional in openapi file
second field is marked as required but not because of the annotation because it is a 'long'

@franck-malka
Copy link
Author

@OpenApiRequired is not valid in java

dzikoysk added a commit that referenced this issue Sep 4, 2024
@dzikoysk
Copy link
Member

dzikoysk commented Sep 4, 2024

It seems to be working fine:

// by default nullable fields are not required, but we can force it
@OpenApiRequired
public String getNullableIsRequired() {
return "required";
}

Could you share your code for more details? :)

@franck-malka
Copy link
Author

franck-malka commented Sep 4, 2024 via email

@dzikoysk
Copy link
Member

dzikoysk commented Sep 4, 2024

I see you put it on getters and setters as well

We don't support this kind of annotations on fields. There's an open ticket if you'd be interested in keeping an eye on this topic tho:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

2 participants