Skip to content

Responses Object keys must be strings? #3068

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

Closed
mabar opened this issue Oct 31, 2022 · 6 comments
Closed

Responses Object keys must be strings? #3068

mabar opened this issue Oct 31, 2022 · 6 comments

Comments

@mabar
Copy link

mabar commented Oct 31, 2022

Responses Object spec says that: This field MUST be enclosed in quotation marks (for example, “200”) for compatibility between JSON and YAML (https://github.com/OAI/OpenAPI-Specification/blob/157a4c81ae537ef793b2bee368bc00d88b461de8/versions/3.0.2.md#patterned-fields-1)

Is it really needed?

Json only allows strings as keys and at least in PHP numeric keys are auto-encoded to strings.

Yaml allows both, but in PHP numeric keys are always type-casted to an int, like 200, instead of "200"

Because it is probably not possible to create workaround for this in PHP, we could solve it only by adding a flag to yaml encoder to make integer keys strings (in case array is not auto-indexed from 0)

But the preferred way would be to remove string key requirement from specification.

@hkosova
Copy link
Contributor

hkosova commented Nov 1, 2022

This was previously discussed in #2147 (comment) and #2171 and the decision was to keep string keys.

@mabar
Copy link
Author

mabar commented Nov 1, 2022

There is already quite a lot of validations dependent on multiple properties or even multiple objects, would it be a big problem to add one more in next major? So far I found no PHP library that prints codes as strings, because it's very hard to do...

@mabar
Copy link
Author

mabar commented Nov 1, 2022

Maybe spec could encourage use of json for cross compatiblity instead of yaml? json works well in practically every language, unlike way more complex yaml.

@handrews
Copy link
Member

handrews commented Nov 6, 2022

Maybe spec could encourage use of json for cross compatiblity instead of yaml? json works well in practically every language, unlike way more complex yaml.

@mabar the OpenAPI specification is defined as a JSON object that can be encoded as either JSON or YAML, so this is pretty much already the case. This is, in fact, why the keys in YAML need to be a string as the issues linked by @hkosova note.

Any time you are maintaining JSON compatibility in YAML (not just for OpenAPI), you have to convert non-string keys to strings before serializing to YAML.

@mabar
Copy link
Author

mabar commented Nov 14, 2022

Well, some work is being done on Symfony side to solve this problem in PHP.

I hope assumption that all numeric keys could be encoded as strings in yaml, except in arrays that are auto-indexed from zero, will work.

Otherwise we would have to write a whole new yaml encoder with AST support for PHP, to be able explicitly tell, which keys should be strings.

@lornajane
Copy link
Contributor

This has been inactive for a while and I don't think it can be improved by changes in OpenAPI itself, so I'm closing this issue. Thanks for the great discussion, some really useful information in this thread.

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

4 participants