-
Notifications
You must be signed in to change notification settings - Fork 8
Add nullable enum support, fix Symfony 7.3 Psalm warnings, and correct class property definitions #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@oddssoft |
📝 Description ✅ Added support for nullable enum types (["string", "null"]) 🛠️ Fixed incorrect use of allOf for class-based properties — replaced with proper $ref or oneOf([$ref, null]) 🔧 Removed deprecated types that caused Psalm errors under Symfony 7.3 ⚙️ Verified compatibility with PHP 8.4 and Symfony 7.3 ✅ Validation fully populated objects objects with some null fields All schemas validated correctly via jsonschemavalidator.net. Below is the generated JSON Schema for the Actor class, including proper handling of nullable fields and nested object references:
Invalid Data – Fails Validation (movies must be an array of valid Movie objects — null is not allowed in the array.)
Minimal Valid Input. Valid: only required fields provided.
Fully Populated Input. Valid
Typical Input with Nullable Fields. Valid: optional fields use null values where allowed.
|
Hi @butschster! Could you please take another look at this PR when you have a moment? |
- Changed enum generation from $ref to inline enum with "type": "string" and explicit "enum" values - Added proper nullable support for enums using ["string", "null"] - Replaced invalid allOf usage for object types with correct $ref - When nullable object, used oneOf with $ref and null type
Hi @butschster! Symfony's property-info version 7.3 is not compatible with previous Symfony versions (6.4 || 7.2). I suggest that in the 1.x branch we lock the symfony/property-info dependency to <7.3, since the current code cannot be built with 7.3 due to Psalm failing on deprecation warnings. At the same time, since I personally need support for Symfony 7.3, I’d like to propose creating a new 2.x branch that drops support for Symfony versions below 7.3 — targeting users who specifically need compatibility with Symfony 7.3+. Let me know what your preferred direction is. I've already pushed the required changes to the 1.x branch — please have another look at the PR when you have a chance.? |
It would be great if someone get union types implementation from context-hub#1 |
@oddssoft I've created 2.x branch and switched target branch |
Thanks for the suggestion! I’ll take a look and try to implement union types support based on the context-hub#1 PR. Will keep you updated on the progress. |
@butschster hi! For version 2.x, I will create a separate PR that refactors the type resolution logic to use The goal is to keep the 1.x branch alive and maintainable until Symfony 8.x is released. |
?string - type: [string, null] not type: string
`final class CoutryDto
{
#[Field(format: Format::DateTime)]
public string $createdAt;
#[Field(format: Format::DateTime)]
public string $updatedAt;
#[Field(default: null, format: Format::DateTime)]
public ?string $deletedAt;
}`
Note: The $deletedAt property is explicitly typed as ?string and should be treated as nullable.
However, the generated schema incorrectly defines deletedAt as a non-nullable string:
{ "properties":{ "createdAt":{ "format":"date-time", "type":"string" }, "updatedAt":{ "format":"date-time", "type":"string" }, "deletedAt":{ "format":"date-time", "type":"string" }, "id":{ "format":"uuid", "type":"string" }, "name":{ "type":"string" }, "patterns":{ "type":"array", "items":{ "$ref":"#/definitions/PatternUpdated" } } }, "required":[ "createdAt", "updatedAt", "id", "name", "patterns" ], "definitions":{ "PatternUpdated":{ "title":"PatternUpdated", "type":"object", "properties":{ "id":{ "type":"string" }, "description":{ "type":"string" } }, "required":[ "id", "description" ] } } }
When validating a CountryDto instance with deletedAt = null, the following error is returned:
[ { "property": "deletedAt", "pointer": "/deletedAt", "message": "NULL value found, but a string is required", "constraint": { "name": "type", "params": { "found": "NULL", "expected": "a string" } }, "context": 1 } ]
While adding support for nullable properties in JSON Schema generation, several related issues were discovered and addressed:
✅ Added support for nullable enum types using ["string", "null"] representation
🛠️ Fixed incorrect usage of allOf for class-based properties — replaced with proper $ref or oneOf([$ref, null])
🚫 Removed deprecated type usages that triggered Psalm errors under Symfony 7.3
🧹 Improved compatibility and reduced warnings in modern environments
These changes improve both the correctness and compatibility of generated schemas and static analysis in modern Symfony setups.