dotnet / dotnet/aspnetcore

Schema transformers receive an ignored property's metadata for a polymorphic discriminator node

Open Beginner friendly
#69,080 1 comment 0 reactions 0 assignees View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

`OpenApiSchemaService` emits the type discriminator of a polymorphic derived type as a one-value string constant, which is correct. When it hands that node to an `IOpenApiSchemaTransformer`, it populates `OpenApiSchemaTransformerContext.JsonPropertyInfo` (and therefore `JsonTypeInfo`) by matching the discriminator's name against the type's JSON metadata. That match can land on a property the emitter itself excluded from the schema because it carries `[JsonIgnore]`.

`context.JsonTypeInfo.Type` then reports the CLR type of an ignored property instead of the type of the node the framework actually built. A transformer that keys on the node's type rewrites the discriminator and silently corrupts it.

The same component already handles the exclusion correctly elsewhere. An ignored property on a non-polymorphic type produces no schema node and no transformer callback. Only the discriminator lookup reaches back into the excluded metadata.

### Expected Behavior

The transformer should not receive an ignored property's metadata for the discriminator node. Either no `JsonPropertyInfo` is supplied for a synthesized discriminator, or the lookup skips the properties the emitter excluded, so `context.JsonTypeInfo.Type` describes the node the framework built.

The control output is the correct document, and it is what a transformer has to be able to leave intact.

### Steps To Reproduce

[aspnetcore-openapi-discriminator-repro](https://github.com/iPilot/aspnetcore-openapi-discriminator-repro)

### Exceptions (if any)

_No response_

### .NET Version

10.0.400

### Anything else?

This block is byte-identical on `release/9.0`, `release/10.0` and `main`, so the .NET 11 preview line — `Microsoft.AspNetCore.OpenApi 11.0.0-preview.7`, which takes `Microsoft.OpenApi [3.9.0, 4.0.0)` — carries it unchanged. I read that from source rather than running it: 3.x is consumed only by the net11.0 packages and I have no .NET 11 SDK to hand.

### Suggested fix
Skip, in that loop, the candidates the emitter itself skipped. A JsonPropertyInfo with no accessors is exactly an ignored one:
```
foreach (var propertyInfo in jsonTypeInfo.Properties)
{
// the schema carries no node for an ignored property, so its metadata describes no node here
if (propertyInfo is { Get: null, Set: null })
{
continue;
}

...
}
```

Contributor guide

Open the contributing guide

Research direction

Locate OpenApiSchemaService and the loop that matches discriminator names against jsonTypeInfo.Properties. Compare its candidates with the emitter's ignored-property handling, then use the linked aspnetcore-openapi-discriminator-repro to verify ignored metadata is not supplied to the transformer and the control document remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend-api-design
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.