-
Notifications
You must be signed in to change notification settings - Fork 832
Description
Description
Currently when a schema is generated for nullable properties, the type
is defined as an array ["null","<property_type>"]
.
This does not follow the standard for OpenAPI 3.0 spec:
- https://spec.openapis.org/oas/v3.0.3#schema-object
type - Value MUST be a string. Multiple types via an array are not supported.
4.7.24.2 Fixed Fields - Should have an additional field name "nullable": boolead
This becomes a problem when API's for Structured Outputs follows the OpenAPI 3.0 standard and some workarounds needs to be put in place as in this PR, having a setting, would be ideal.
Reproduction Steps
Reproduction Code
using Microsoft.Extensions.AI;
var schema = AIJsonUtilities.CreateJsonSchema(typeof(Class1));
Console.WriteLine(schema.GetRawText());
class Class1
{
public string? Age { get; set; }
}
Output:
{
"type": "object",
"properties": {
"age": {
"type": [
"string",
"null"
]
}
},
"additionalProperties": false,
"required": [
"age"
]
}
Expected behavior
A SchemaGeneration option that comply with OpenAPI 3.0 spec, generating one type and an additional Nullable boolean field in the schema.
Following the RFC (https://spec.openapis.org/oas/v3.0.3#schema-object)
Output:
{
"type": "object",
"properties": {
"age": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false,
"required": [
"age"
]
}
Actual behavior
Output:
{
"type": "object",
"properties": {
"age": {
"type": [
"string",
"null"
]
}
},
"additionalProperties": false,
"required": [
"age"
]
}
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
When using other types (not string) this gets trickier as the array contains multiple types for the same property.
Using int?
example:
{
"type": "object",
"properties": {
"age": {
"type": [
"string",
"integer",
"null"
]
}
},
"additionalProperties": false,
"required": [
"age"
]
}