Set additionalProperties when [JsonExtensionData] is present #56767
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reacting to feedback in #56318 (comment) and discussion in #56707.
The
additionalProperties
keyword in a schema is used to indicate that a schema can match against properties explicitly defined in theschema.properties
list and additional unspecified properties.The JSON Schema specification that STJ's JsonSchemaExporter targets assumes that
additionalProperties
istrue
by default for all schemas, meaning that a schema for a type will always be able to capture properties that are not explicitly defined in the properties list (see here).The OpenAPI specification (as of v3 that we target) assumes the opposite.
additionalProperties
is assumed false and must be explicitly enabled in order to indicate that a schema can capture properties not explicitly defined. Furthermore, OpenAPI doesn't support a valuetrue
for the schema to support catch-alls. It requires that you implement an empty object to indicate that additional property values can match any type.These two problems come together like Voltron in our handling of
[JsonExtensionData]
attributes on property.JsonSchemaExporter
assumes that additionalProperties is implicitly true and doesn't set it when it generates a schema. We have to patch this mismatch in ourTransformSchemaNode
implementation to comply with what OpenAPI expects.Since
[JsonExtensionData]
can only ever be applied onDictionary<string, object>
andDictionary<string, JsonElement>
, we don't have to worry about specifying concrete schemas for the values of additional properties. The empty object will do.