Handle nullability more accurately#2933
Conversation
67ef969 to
937b8eb
Compare
937b8eb to
a70df43
Compare
baywet
left a comment
There was a problem hiding this comment.
Getting closer (I'm only focusing my review on schema object field, and deserialization so far)
|
To recap our call, and previous discussions There shouldn't be any field/state for nullability on the OpenAPISchema model, only in the deserialization phases. OpenAPI 3.1/3.2serialization and deserialization should be symmetrical, serialize type null as part of the type property, array or single. OpenAPI v3.0serializationSerialize type null (no other types) as enum null because nullable true by itself is ignored and the resulting schema would validate anything. deserializationDeserialize type something to something Once both properties are deserialized:
Swagger 2.0serializationalways serialize type null (with or without something else) as x-nullable: true. That's because :
DeserializationDo the same mark and resolve behaviour as V3.0 for consistency, but for the x-nullable extension. |
| hasOneOfNullAndSingleEnumWith3_0 = nullInOneOf && effectiveOneOf is { Count: 1 } && | ||
| effectiveOneOf[0].Enum is { Count: > 0 }; | ||
| // If we have a schema that's only just { "type": "null" }, we serialize it as enum with null value. | ||
| if (Type == JsonSchemaType.Null && OneOf is not { Count: > 0 } && AnyOf is not { Count: > 0 } && Enum is not { Count: > 0 }) |
There was a problem hiding this comment.
should this also include all of?
|
|
||
| if (schema.Extensions is not null && schema.Extensions.ContainsKey(OpenApiConstants.NullableExtension)) | ||
| var extensions = schema.Extensions; | ||
| if (extensions?.TryGetValue(OpenApiConstants.NullableExtension, out var nullExtRawValue) == true && |
There was a problem hiding this comment.
use metadata instead of extensions
My previous PR was trying to handle nullability for enums specifically. This PR is more generalized to handle nulls more correctly as it was still buggy for non-enums.
In addition, the previous fix for enums didn't also fix all enum scenarios. It only fixed it when we are given "oneOf (null, schemaWithEnum)". But if we are given a schema reference instead, we are not going to add "null" to the reference and we will produce the wrong thing.
I'll leave comments on some specific updated tests to explain.