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

openapi: simplify API consumer interface, make output fields required #662

Merged
merged 2 commits into from
Aug 9, 2024

Conversation

chirino
Copy link
Contributor

@chirino chirino commented Aug 8, 2024

Keep input fields optional.

@ctron
Copy link
Contributor

ctron commented Aug 9, 2024

I am not a fan of this. I think a rule of "if it's absent, it's empty" is easy to understand.

Now we are introducing the idea of "input" and "output" models. Having no clear indication of which is which, but expecting different policies around that. Even removing the attributes for deserializing defaults.

@chirino
Copy link
Contributor Author

chirino commented Aug 9, 2024

The problem is NOT that it's hard to understand. It is harder to use. All API clients need to null-check every field access that is not required since it could be missing. It's the same reason you use field Vec<X> declarations instead of field Option<Vec<T>> declarations for all of these. It's much easier to deal with empty values than null values. You avoid an additional "does it exist check".

The only valid reason to make these not-required fields is if it's an experimental field that may be removed. If that's the case we should be documenting that fact too.

Could we keep the input and output models in different files to distinguish them?

@ctron
Copy link
Contributor

ctron commented Aug 9, 2024

Hm, maybe I just don't see the issue outside of Rust. That might be case.

I would expect that the benefit overall is, that empty stuff doesn't get serialize and reduces output. Meaning: less traffic. Maybe not worth optimizing for that. 🤷

Could we keep the input and output models in different files to distinguish them?

I guess having different modules might make sense then. I am still not a fan, but at least that would make it transparent why some group of structs follow one pattern, while others follow another one.

@bobmcwhirter
Copy link
Contributor

@chirino would it be troublesome if we emit empty vectors, but skip any Option::None? Does that only solve half the problem?

For me, seeing:

average_score: null,

seems not much better than just omitting it, and you've still got to null-check.

While I do see the benefit of

list_of_things: [],

avoids a null-check.

@chirino
Copy link
Contributor Author

chirino commented Aug 9, 2024

@chirino would it be troublesome if we emit empty vectors, but skip any Option::None? Does that only solve half the problem?

That would be fine. Consumer code still has to do similar existence checks. So ease of use is about the same.

But I still question why we don't want the openapi models to match what our Rust models are. We are actively trying to make them different. Saying fields could be missing is like saying our fields are being held in HashMap and so they may not be there when you read them back out.

Like why are we making extra effort to make things more complex?

@chirino
Copy link
Contributor Author

chirino commented Aug 9, 2024

Seems like Utopia does not make the Option fields required (I think it's a bug), so there is there is actually no difference.

BTW if Utopia was doing the right thing. It would impact client-generated types.. field names become average_score? vs average_score to denote that the field may be undefined as well as null.

@chirino
Copy link
Contributor Author

chirino commented Aug 9, 2024

Updated PR to only apply this change to Vecs and Labels.

Keep input fields optional.


Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
@chirino
Copy link
Contributor Author

chirino commented Aug 9, 2024

Ah look like we COULD make options required with utopia. see: juhaku/utoipa#530

Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
@bobmcwhirter
Copy link
Contributor

@chirino would it be troublesome if we emit empty vectors, but skip any Option::None? Does that only solve half the problem?

That would be fine. Consumer code still has to do similar existence checks. So ease of use is about the same.

But I still question why we don't want the openapi models to match what our Rust models are. We are actively trying to make them different. Saying fields could be missing is like saying our fields are being held in HashMap and so they may not be there when you read them back out.

Like why are we making extra effort to make things more complex?

I think that's a fair assessment also. I don't have a strong opinion really.

@chirino
Copy link
Contributor Author

chirino commented Aug 9, 2024

Latest commit to the PR adds back making the optional fields required. This time, adding the right utopia config so that it shows up in the openapi doc.

@bobmcwhirter
Copy link
Contributor

Right on.

@bobmcwhirter
Copy link
Contributor

Merge at will.

@chirino chirino added this pull request to the merge queue Aug 9, 2024
Merged via the queue into trustification:main with commit 48b79a1 Aug 9, 2024
1 check passed
@chirino chirino deleted the simpler-api branch August 9, 2024 17:19
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

Successfully merging this pull request may close these issues.

3 participants