Repository navigation
Clarify nullable behaviour - #4094
Conversation
Follow-up to #3976: - Add clarifying comments. - Fix asymmetry with handling `anyOf` and `oneOf`.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4094 +/- ##
==========================================
+ Coverage 95.08% 95.11% +0.02%
==========================================
Files 111 111
Lines 4046 4070 +24
Branches 820 832 +12
==========================================
+ Hits 3847 3871 +24
Misses 199 199
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Human review recommended
It changes core schema nullability behavior across composed-schema shapes and spec-version serialization semantics, which warrants a final human validation pass despite the added tests.
Pull request overview
This PR is a follow-up to #3976 that clarifies and verifies nullability behavior for composed schemas, specifically addressing symmetry between nullable and non-nullable handling when schemas use anyOf, oneOf, or allOf, and ensuring serialization remains sensible across OpenAPI/Swagger versions.
Changes:
- Add non-nullable “mirror image” behaviors for composed schemas by removing
nullfromanyOf/oneOfmembers (or clearing theNullflag) when a member is non-nullable. - Add targeted unit tests for non-nullable composed-schema scenarios and for dictionary value schemas that intentionally omit
type(andnullable) across OpenAPI 3.1/3.0/Swagger 2.0. - Add clarifying inline documentation in both
SchemaGeneratorandAnnotationsSchemaFilterdescribing where nullability is represented depending on schema shape.
File summaries
| File | Description |
|---|---|
| test/Swashbuckle.AspNetCore.TestSupport/Fixtures/JsonSerializerSchemaGeneratorTestTypes.cs | Adds non-nullable fixture types used to validate composed-schema nullability behavior. |
| test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs | Adds tests covering non-nullable removal of null from anyOf/oneOf, and serialization expectations for “no type” schemas across spec versions. |
| test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsSchemaFilterTests.cs | Adds tests verifying SwaggerSchema(Nullable=false) removes null from compositions and refactors the test helper to support multiple declaring types. |
| src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs | Implements non-nullable “mirror” behavior by removing/clearing null within anyOf/oneOf when nullable=false, with explanatory comments. |
| src/Swashbuckle.AspNetCore.Annotations/AnnotationsSchemaFilter.cs | Mirrors the same non-nullable composition handling for annotation-driven nullability, plus clarifying comments. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
Follow-up to #3976:
anyOfandoneOf.