fix: null values in examples, example and default (#4339, #4229) by ewaostrowska · Pull Request #5060 · swagger-api/swagger-core (original) (raw)
This pull request enhances how OpenAPI 3.1 Schema objects handle and serialize null and literal "null" values for default, example, and examples fields. The changes ensure correct distinction between a missing field, an explicit null, and the string "null", especially when nullable=true. This improves both the internal model and the generated OpenAPI output, and aligns the code with the OpenAPI 3.1 specification.
Key changes:
Handling of null and "null" values for default and example:
- Updated
ModelResolverandAnnotationsUtilsto interpret the string"null"as a Javanullonly whennullable=true; otherwise,"null"is treated as a literal string. This logic is applied for bothdefaultValueandexampleattributes. - Added a new
defaultSetFlagto theSchemamodel to track when the default value is explicitly set tonull(vs. not set at all), mirroring the existingexampleSetFlag.
Serialization improvements:
- Modified
SchemaSerializerandSchema31Serializerto ensure that ifexampleordefaultis explicitly set tonull, the serialized JSON includes"example": nullor"default": nullfields, rather than omitting them.
Consistent parsing and handling ofexamplesarrays: - Added a new utility method
parseExamplesArrayto parse each entry in theexamplesarray, converting"null"tonullwhen appropriate, and returning parsed JSON objects/arrays or the original string as needed. - Updated all relevant code paths to use this method for setting the
examplesproperty, and ensured this logic is only applied for OpenAPI 3.1.
These changes improve the fidelity of OpenAPI schema generation and serialization, especially regarding explicit null handling and the distinction from the literal string "null".