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

openapi3: ensure YAML marshalling matches JSON's #943

Merged
merged 1 commit into from
Jun 2, 2024

Conversation

damien-talos
Copy link
Contributor

I noticed that some types used MarshalYAML as the source of truth for MarshalJSON, but others didn't.
Some of those were out of sync, especially with regards to the serialization of extensions.

This PR updates all the MarshalJSON functions to call the MarshalYAML one, with the result that both JSON and YAML marshalling should result in the same value being written.

@fenollp
Copy link
Collaborator

fenollp commented Apr 28, 2024

Hi! Thanks for submitting this. Should we be thinking of a regression test?
Please rebase your PR, otherwise LGTM :)

@damien-talos
Copy link
Contributor Author

@fenollp Certainly, I'll rebase my PR.
I'm happy to add tests, though I'm not sure what that would look like?
Perhaps somehow generating tests that serialize various objects to both JSON and YAML, than de-serialize them, and compare them to ensure they are equivalent? Is there existing code to generate tests like that, or would they have to be hand-coded?

@fenollp
Copy link
Collaborator

fenollp commented Apr 29, 2024

Such tests already exist, I was just wondering if you'd thought of something new. No worries, let's just rebase :)

@damien-talos
Copy link
Contributor Author

I did a rebase here, and fixed a few issues, but I've now got tests that are failing because the properties are serialized in a different order.
How important is the order of the properties in the serialized output? I could update the snapshots to reflect the new order (which seems to be alphabetical), but I'm not sure if that's desired or not?

@fenollp
Copy link
Collaborator

fenollp commented May 11, 2024

I believe you forgot to push your rebase?

On keys ordering: theoretically order doesn't matter. Now, we have users requesting certain orderings: "as written", "alphabetical" and custom.
Could you show which tests now fail due to ordering / rebase this PR?
Note you can use JSONEq.

@damien-talos damien-talos force-pushed the yaml-marshalling-matches-json branch from 82e3fb7 to 399bf50 Compare May 13, 2024 15:08
@damien-talos
Copy link
Contributor Author

I believe you forgot to push your rebase?

On keys ordering: theoretically order doesn't matter. Now, we have users requesting certain orderings: "as written", "alphabetical" and custom. Could you show which tests now fail due to ordering / rebase this PR? Note you can use JSONEq.

There's quite a number of tests that are failing due to these changes.
I've got some work-in-progress locally to resolve these, by simply updating the snapshots, but I've also done some work to use an ordered map for the serialization, to give full control over the serialization / deserialization everywhere. The only downside to that is that if I replace the existing maps with the ordered map (e.g. for properties, additional properties, extensions, etc.) that breaks the existing api (I've been using go-ordered-map for this).

If this is something that interests you, I can continue on with that work in a separate PR, to at least provide a foundation / exploration of how that ordered marshalling / unmarshalling might look.

@damien-talos damien-talos force-pushed the yaml-marshalling-matches-json branch 8 times, most recently from 034224a to 319ab87 Compare May 13, 2024 16:01
@fenollp
Copy link
Collaborator

fenollp commented May 13, 2024

Cool! I have one myself you can pull from if you'd like: #695

But hm I'll try to merge this one first. Maybe for now we can just sort the fields, so that only YAML marshaling sees ordering changes. Did I get that right?
I'll dive deeper some time at the end of the week hopefully

@damien-talos damien-talos force-pushed the yaml-marshalling-matches-json branch from 319ab87 to fb29602 Compare May 13, 2024 16:04
@damien-talos
Copy link
Contributor Author

Okay, sorry for all the pushes, but I think I've fixed the tests and the PR build checks now.

@fenollp fenollp changed the title Ensure that YAML marshalling matches the JSON marshalling openapi3: ensure YAML marshalling matches JSON's Jun 2, 2024
@fenollp fenollp merged commit 03281ec into getkin:master Jun 2, 2024
7 checks passed
@fenollp
Copy link
Collaborator

fenollp commented Jun 2, 2024

Thanks for this!

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.

2 participants