Fix #3275: prevent @Nullable $ref field from mutating shared component schema type#3295
Fix #3275: prevent @Nullable $ref field from mutating shared component schema type#3295s-chan-o wants to merge 1 commit into
Conversation
… component schema type When swagger-core 2.2.44 processes a @nullable field whose type resolves to a $ref, it mutates the shared Schema instance in the components map by setting types = {"null"}. This causes the referenced schema (e.g. Inner) to lose its original type:"object" and appear as type:"null" in the final output. Fix: in handleComponentSchemaTypes(), call SpringDocUtils.fixNullMutatedObjectSchema() which restores any component schema whose type was wrongly reduced to "null"-only when it still has properties (i.e. it is an object). Also fix test sub-module pom.xml versions from 2.8.17-SNAPSHOT to 2.8.18-SNAPSHOT to match the root pom after the maven-release-plugin bump.
|
Note: I am only a contributor to this project, and not associated with it I strongly disagree with this PR being merged. The issue is entirely tied to swagger-core, and there is a suggested fix there. Rather than merging temporary logic to this repository and having to manage the lifecycle and removal of it, I would suggest that you instead inject the fix locally on your end while waiting for the upstream adjustment (with a custom Resolver bean for example). |
|
Thank you for the context, @Mattias-Sehlstedt — that's a fair point architecturally. I checked swagger-api/swagger-core#5124 and it is currently labeled backlog with no assigned reviewer or milestone, so the timeline for an upstream fix is unclear. In the meantime, users on Spring Boot 4 + swagger-core 2.2.44 are hitting this silently — component schemas resolve to I'll leave the final call to the maintainer: if a temporary guard in |
Problem
When a record field is annotated with
@Nullableand its type resolves to a$ref(i.e. another component schema), swagger-core 2.2.44 mutates the sharedSchemainstance stored in the components map by settingtypes = {"null"}. This causes the referenced schema (e.g.Inner) to lose its originaltype: "object"declaration and appear astype: "null"in the final OpenAPI output.Reproducer (OAS 3.1):
Before fix –
Innerschema in components:Fix
In
AbstractOpenApiResource.handleComponentSchemaTypes(), call the new helperSpringDocUtils.fixNullMutatedObjectSchema()which restores any component schema whosetypeswas wrongly reduced to{"null"}when it still hasproperties(i.e. it is clearly an object).After fix:
Test
Added regression test
SpringDocApp175Test(OAS 3.1, javadoc variant) with:Innerrecord (the referenced schema)Outerrecord with@Nullable Inner resultNullableRefControllerexposingGET /outerresults/3.1.0/app175.jsonassertingInnerhastype: "object"Also bumped test sub-module
pom.xmlversions from2.8.17-SNAPSHOT→2.8.18-SNAPSHOTto match the root pom after themaven-release-pluginiteration bump.Fixes #3275