Skip to content

OpenAPI: put record constructor defaults on the $ref, not the shared component - #69863

Open
sander1095 wants to merge 2 commits into
dotnet:mainfrom
sander1095:sander1095-openapi-record-ctor-default-enum-ref
Open

sander1095 wants to merge 2 commits into
dotnet:mainfrom
sander1095:sander1095-openapi-record-ctor-default-enum-ref

Conversation

@sander1095

Copy link
Copy Markdown
Contributor

OpenAPI: put record constructor defaults on the $ref, not the shared component

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Stop 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:

app.MapPost("/tickets",      (CreateTicketRequest r) => { });
app.MapPut("/tickets/{id}",  (int id, UpdateTicketRequest r) => { });

[JsonConverter(typeof(JsonStringEnumConverter<Priority>))]
public enum Priority { Low, Normal, High }

public record CreateTicketRequest(string Title, Priority Priority = Priority.Normal);
public record UpdateTicketRequest(string Title, Priority Priority);
// Before, CreateTicketRequest mapped first
"Priority":            { "enum": ["Low", "Normal", "High"], "default": "Normal" },  // ❌ UpdateTicketRequest gets it too
"CreateTicketRequest": { … "priority": { "$ref": "#/components/schemas/Priority" } },
"UpdateTicketRequest": { … "priority": { "$ref": "#/components/schemas/Priority" } }

// Before, UpdateTicketRequest mapped first: no "default" anywhere ❌

// After, either order
"Priority":            { "enum": ["Low", "Normal", "High"] },
"CreateTicketRequest": { … "priority": { "$ref": "#/components/schemas/Priority", "default": "Normal" } },
"UpdateTicketRequest": { … "priority": { "$ref": "#/components/schemas/Priority" } }

Cause

JsonSchemaExporter writes a constructor default straight into the property's default keyword. When that schema becomes a component, the default goes with it. [DefaultValue] doesn't have this problem: since #64109 it goes into the internal x-ref-default annotation, which ends up on the $ref.

This is a 10.0 regression: 9.0 emitted separate Priority and Priority2 components, but since #59480 (10.0-preview1) components are keyed by name only, so one Priority serves both properties.

The fix

In OpenApiSchemaService.TransformSchemaNode, when a property's schema will be componentized and its constructor parameter has a default, move STJ's default into x-ref-default. This is the last step that still knows the default came from the property, not the type. Inline schemas are unchanged.

Tests

  • New theory ConstructorParameterDefaultIsAppliedToReferenceRegardlessOfMappingOrder in OpenApiSchemaReferenceTransformerTests.cs, with one row per mapping order.
  • Without the fix, both rows fail: one because the default leaks onto the shared component, the other because the $ref has no default.
  • dotnet test src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests: 1035 passed, 5 skipped, 0 failed.
  • Checked by hand, not in a test: [DefaultValue] on the property still overrides the constructor default.

Follow-up idea: once #69861 merges, its deprecated handling and this default handling could share one helper that moves a property-level keyword to its x-ref-* annotation.

Fixes #69862

Sander ten Brinke and others added 2 commits October 16, 2026 00:12
…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>
Copilot AI balanced review requested due to automatic review settings October 15, 2026 22:53
@github-actions github-actions Bot added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Oct 15, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 15, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @sander1095. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 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-default for 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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 23, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 07b78cbe Deployed Oct 15, 2026 by dotnet-policy-service[bot] via pat_pool #1861
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI: record constructor default is written to the shared enum component instead of the $ref

2 participants