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

fix: OpenAPI 3.1: required with type: [array, null] #2216

Merged
merged 6 commits into from
Dec 14, 2024

Conversation

joscha
Copy link
Contributor

@joscha joscha commented Dec 11, 2024

OpenAPI 3.1 allows a union type field for properties. Such a union can be:

type:
  - array
  - 'null'

for example.
Currently the generator ignores this when the field is required at the same time.
The required condition, however, is only applied to the field itself, e.g. an object with a required field x will need to contain the field, but it doesn't define what the value of that field is. The type field defines valid values (in the above case the field can be of type array or of type null.
Thus, a field can be nullable at the same time as being required.

I added a sample file and a potential fix.
The relevant section in the spec is here.
A real-world example of such a spec can be found here: https://github.com/planet-a-ventures/affinity-node/blob/8dd03eb299ff7d08c88405a95627dbc47280c3c5/openapi/2024-11-18.json#L2027-L2039



class Basket(BaseModel):
apples: List[Apple] | None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

previously, before the change in this PR, this would have been:

Suggested change
apples: List[Apple] | None
apples: List[Apple]

items:
$ref: '#/components/schemas/Apple'
required:
- apples
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(1) the property is required, e.g. it must exist in the payload.

Comment on lines +16 to +17
- array
- 'null'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(2) but it's value can be either an array of Apples or null.

@joscha
Copy link
Contributor Author

joscha commented Dec 12, 2024

@koxudaxi I am somewhat blocked by this - is there any chance we could discuss this issue, please, so I can try to gauge whether this can land and/or to use a fork and/or to use a patch file for the result?

Copy link

codspeed-hq bot commented Dec 14, 2024

CodSpeed Performance Report

Merging #2216 will not alter performance

Comparing joscha:joscha/required-nullable (8b74247) with main (8eccc15)

Summary

✅ 30 untouched benchmarks
🆕 1 new benchmarks

Benchmarks breakdown

Benchmark main joscha:joscha/required-nullable Change
🆕 test_main_openapi_nullable_31 N/A 50.7 ms N/A

Copy link

codecov bot commented Dec 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (8eccc15) to head (8b74247).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2216   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           38        38           
  Lines         4285      4288    +3     
  Branches       987       988    +1     
=========================================
+ Hits          4285      4288    +3     
Flag Coverage Δ
unittests 99.67% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@koxudaxi koxudaxi merged commit 7782e51 into koxudaxi:main Dec 14, 2024
28 checks passed
@koxudaxi
Copy link
Owner

@joscha

@koxudaxi I am somewhat blocked by this - is there any chance we could discuss this issue, please, so I can try to gauge whether this can land and/or to use a fork and/or to use a patch file for the result?

Thank you for creating the PR.
I wiil relase the PR in this weekend.

I'm waiting for the PR will be marged.
#2165

@joscha
Copy link
Contributor Author

joscha commented Dec 14, 2024

@joscha

@koxudaxi I am somewhat blocked by this - is there any chance we could discuss this issue, please, so I can try to gauge whether this can land and/or to use a fork and/or to use a patch file for the result?

Thank you for creating the PR.
I wiil relase the PR in this weekend.

I'm waiting for the PR will be marged.
#2165

Amazing, thank you!

@joscha joscha deleted the joscha/required-nullable branch December 15, 2024 20:25
@joscha
Copy link
Contributor Author

joscha commented Dec 15, 2024

Thank you for 0.26.4 @koxudaxi!

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