Skip to content

Use default value for ref property #1202

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

github-abcde
Copy link

When the OpenAPI spec contains an optional enum property with a default value, the default value is ignored and is always set to UNSET in the generated client (current behavior).

This PR attempts to address this, checking if a default is defined for a referenced property and applying that. If no default is defined on the property itself, the current behavior is applied (fetching the default from the parent if present and falling back to None (UNSET) if absent).

I hope I've provided everything required in this PR, but if not, please let me know :-)

@github-abcde github-abcde force-pushed the use_default_value_for_ref_property branch from 6fa4204 to 5d34403 Compare March 13, 2025 13:27
Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the OpenAPI spec, we're supposed to ignore any additional properties defined on a reference—which would include default unless I'm missing something.

The "right" way to do this might be with allOf?

@github-abcde
Copy link
Author

According to the OpenAPI spec, we're supposed to ignore any additional properties defined on a reference—which would include default unless I'm missing something.

The "right" way to do this might be with allOf?

Sorry for not getting back to you earlier. I needed a bit of time to deeper understand the reference you made, since it seemed quite straightforward (but contradicts what I wanted it to say :))

However (and I'm still not entirely sure I've wrapped my head around this correctly) there is a distinction made between "Reference objects" and "Schema objects that contain a $ref keyword".
After some searching, I found this: https://learn.openapis.org/referencing/overview.html#a-taxonomy-of-references.

Isn't this an example of the 4th row in the table, whereas a direct reading of the spec would be an example of the 2nd row in the table?
The remark that 'this is more-or-less equivalent to using a one-element "allOf"' seems to agree with your suggestion to use allOf.

Another interesting fact is that this schema structure is generated by pydantic (from at least 2.8 up to 2.11.3 which is the latest version at this time of writing). So it seems that it's either a bug in how pydantic generates the schema, or how we interpret it :)
What do you think?

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