Skip to content

Conversation

eiriktsarpalis
Copy link
Member

The JsonSchemaExporter implementation uses singletons to represent boolean schema nodes, however this approach does not account for the possibility of a transformer context registering itself to the singleton. Should be backported to 9.

Fix #109868.

public static JsonSchema False { get; } = new(false);
public static JsonSchema True { get; } = new(true);
public static JsonSchema CreateFalseSchema() => new(false);
public static JsonSchema CreateTrueSchema() => new(true);
Copy link
Member

Choose a reason for hiding this comment

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

And we don't just want to use new(true) at call sites?

Copy link
Member Author

Choose a reason for hiding this comment

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

The constructor accepting boolean is marked private since boolean schemas are special and do not admit keywords. It's better if the false and true schemas are created explicitly. If I were to redesign this type today, I would have made booleans and objects separate subclasses -- the current arrangement has been the source of most bugs we've seen in JsonSchemaExporter.

Copy link
Member

Choose a reason for hiding this comment

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

The constructor accepting boolean is marked private since boolean schemas are special and do not admit keywords.

But aren't these factories now 100% equivalent to the constructor? If it's ok for these factories to exist, I don't understand why the constructor is any more problematic.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think JsonSchema schema = new(false); adequately conveys that a false schema is being created. I would rather assign a distinct factory for that case.

@eiriktsarpalis eiriktsarpalis merged commit c468ff1 into dotnet:main Nov 19, 2024
83 checks passed
@eiriktsarpalis eiriktsarpalis deleted the fix-schemaexporter-transformer branch November 19, 2024 19:22
@eiriktsarpalis
Copy link
Member Author

/backport to release/9.0-staging

Copy link
Contributor

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/11920223361

mikelle-rogers pushed a commit to mikelle-rogers/runtime that referenced this pull request Dec 10, 2024
…otnet#109954)

* Fix transformer handling of boolean schemas in JsonSchemaExporter.

* Address feedback.
@github-actions github-actions bot locked and limited conversation to collaborators Dec 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom JsonConverter breaks context variable in JsonSchemaExporterOptions.TransformSchemaNode callback
2 participants