Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Expose polymorphic base type in JsonSchemaExporterContext #104046

Open
captainsafia opened this issue Jun 26, 2024 · 1 comment
Open
Assignees
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation area-System.Text.Json blocking Marks issues that we want to fast track in order to unblock other important work
Milestone

Comments

@captainsafia
Copy link
Member

Background and motivation

For some derivations of JSON schema (particularly OpenAPI), it is necessary to modify the schemas generated by STJ for polymorphic types to correspond with the constraints of the spec. For example, OpenAPI prohibits subschemas that don't contain a type discriminator for a polymorphic type.

To aid implementers in complying with these constraints, a new property should be added to JsonSchemaExporterContext to support resolving the base type a derived type is being generated for. Currently, the only way to determine if we are generating a schema for a derived type is by inspecting the JsonPath. This doesn't give us any information about the base type that we are associated with.

API Proposal

namespace System.Text.Json.Schema;

public readonly struct JsonSchemaExporterContext
{
    public JsonTypeInfo? BaseTypeInfo { get; }
}

API Usage

JsonSchemaExporterOptions configuration = new()
{
	TransformSchemaNode = (context, schema) =>
	{
		if (context.BaseTypeInfo is {} baseType)
		{
			if (baseType.PolymorphismOptions.DerivedTypes.SingleOrDefault(type => type == baseType) is { } baseTypeInDerived)
			{
				if (baseTypeInDerived.TypeDiscriminator == null) return new JsonObject();
			}
		}
	}
};

Alternative Designs

No response

Risks

No response

@captainsafia captainsafia added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 26, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 26, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Jun 26, 2024
@eiriktsarpalis eiriktsarpalis added this to the 9.0.0 milestone Jun 26, 2024
@eiriktsarpalis eiriktsarpalis added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jun 26, 2024
@eiriktsarpalis eiriktsarpalis self-assigned this Jun 26, 2024
@eiriktsarpalis eiriktsarpalis added the blocking Marks issues that we want to fast track in order to unblock other important work label Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation area-System.Text.Json blocking Marks issues that we want to fast track in order to unblock other important work
Projects
None yet
Development

No branches or pull requests

2 participants