Repository navigation
OpenAPI: put record constructor defaults on the $ref, not the shared component - #69863
sander1095 wants to merge 2 commits into
Conversation
…d component JsonSchemaExporter writes the default value of a constructor parameter associated with a property into the property's schema node. When that schema is componentized (e.g. an enum), the default ended up on the shared component, so it either leaked to every property referencing it or was dropped, depending on endpoint mapping order. Move the default into the x-ref-default annotation for componentized schemas, matching how [DefaultValue] is already handled, so it is applied to the schema reference instead. Fixes dotnet#69862 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
WillBeComponentized() already indexes into the schema, which throws for non-object nodes, so the type check was always true. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Thanks for your PR, @sander1095. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
🔵 Needs a closer look
Required frozen review evidence and independent validation were unavailable, leaving approval checks incomplete.
0 open findings
What changed in this PR
Keeps record constructor defaults on property references instead of shared OpenAPI components, addressing mapping-order-dependent leakage or loss.
Changes:
- Moves constructor defaults into
x-ref-defaultfor componentized properties. - Applies the relocation during schema transformation.
- Adds regression coverage for both endpoint mapping orders.
| File | Description |
|---|---|
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/Implementations/OpenApiSchemaReferenceTransformerTests.cs | Tests reference defaults and clean shared components in both mapping orders. |
| src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs | Invokes default relocation during property schema processing. |
| src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs | Adds the constructor-default relocation helper. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
OpenAPI: put record constructor defaults on the
$ref, not the shared componentStop record constructor defaults leaking onto shared components or vanishing.
Description
A record constructor default on a property whose type becomes a component (such as an enum) is written onto the shared component. Depending on endpoint mapping order, the default either leaks to every other use of that type or disappears:
Cause
JsonSchemaExporterwrites a constructor default straight into the property'sdefaultkeyword. When that schema becomes a component, the default goes with it.[DefaultValue]doesn't have this problem: since #64109 it goes into the internalx-ref-defaultannotation, which ends up on the$ref.This is a 10.0 regression: 9.0 emitted separate
PriorityandPriority2components, but since #59480 (10.0-preview1) components are keyed by name only, so onePriorityserves both properties.The fix
In
OpenApiSchemaService.TransformSchemaNode, when a property's schema will be componentized and its constructor parameter has a default, move STJ'sdefaultintox-ref-default. This is the last step that still knows the default came from the property, not the type. Inline schemas are unchanged.Tests
ConstructorParameterDefaultIsAppliedToReferenceRegardlessOfMappingOrderinOpenApiSchemaReferenceTransformerTests.cs, with one row per mapping order.$refhas no default.dotnet test src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests: 1035 passed, 5 skipped, 0 failed.[DefaultValue]on the property still overrides the constructor default.Follow-up idea: once #69861 merges, its
deprecatedhandling and thisdefaulthandling could share one helper that moves a property-level keyword to itsx-ref-*annotation.Fixes #69862