Skip to content

Conversation

@Maxcastel
Copy link
Contributor

@Maxcastel Maxcastel commented Dec 23, 2025

Q A
Branch? 4.2
Tickets Closes #7536
License MIT

ApiResource examples:

#[ApiResource(
    operations: [
        new Get(
            normalizationContext: [
                'attributes' => ['title', 'name', 'author' => ['name']],
            ],
        ),
        new Post(
            denormalizationContext: [
                'attributes' => ['title', 'name', 'author' => ['name']], 
            ],
            normalizationContext: [
                'attributes' => ['id', 'title', 'author' => ['id', 'name']],
            ],
        )
    ]
)]
class Book
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[Groups(['book:read'])]
    #[ORM\Column(type: 'string', length: 255)]
    public string $title = 'The book title';

    #[ORM\Column(type: 'string', length: 255)]
    private ?string $name = null;

    #[ORM\ManyToOne(targetEntity: Author::class)]
    #[ORM\JoinColumn(nullable: false)]
    private Author $author;
    
    #[ORM\Column(type: 'string', length: 20)]
    public string $isbn = '978-3-16-148410-0';

    // getters and setters
}
#[ApiResource]
class Author
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[Groups(['user:read', 'user:write'])]
    #[ORM\Column(type: 'string', length: 255)]
    private string $name;

    // getters and setters
}

Before

API response

/api/books/id GET:
api-books-id

API Docs

api-docs-Book-api_books_id_get

api-docs-Book-api_books_post

JSON Schema

bin/console api:openapi:export --output=swagger_docs.json

"application/json": {
    "schema": {
        "$ref": "#/components/schemas/Book"
    }
}
"Book": {
    "type": "object",
    "properties": {
        "id": {
            "readOnly": true,
            "type": "integer"
        },
        "title": {
            "default": "The book title",
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "author": {
            "type": "string",
            "format": "iri-reference",
            "example": "https://example.com/"
        },
        "isbn": {
            "default": "978-3-16-148410-0",
            "type": "string"
        }
    }
}

Now

API response

/api/books/id GET:
api-books-id

API Docs

api-docs-Book-api_books_id_get2

api-docs-Book-api_books_post2

JSON Schema

"application/json": {
    "schema": {
        "$ref": "#/components/schemas/Book-title_name_author.name"
    }
}
"Book-title_name_author.name": {
    "type": "object",
    "properties": {
        "title": {
            "default": "The book title",
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "author": {
            "$ref": "#/components/schemas/Author-name"
        }
    }
}
"Author-name": {
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        }
    }
}

@Maxcastel Maxcastel changed the title fix(JsonSchema): add support for attributes fix: add support for normalization/denormalization with attributes Dec 23, 2025
@soyuka
Copy link
Member

soyuka commented Dec 26, 2025

This looks nice, can you add a test on the JsonSchema generation (tests/Functional/JsonSchema/JsonSchemaTest.php) as I'm not sure that we'd gather the correct properties.

@Maxcastel Maxcastel force-pushed the fix/json-schema-api-docs-serialization-attributes branch from cda5443 to 36c1db8 Compare December 26, 2025 09:03
@Maxcastel
Copy link
Contributor Author

I rebased onto 4.2 to retrieve the code that fixes the 8 failing tests.

@Maxcastel Maxcastel force-pushed the fix/json-schema-api-docs-serialization-attributes branch from d3a767a to 4691c25 Compare December 26, 2025 20:17
@Maxcastel
Copy link
Contributor Author

This looks nice, can you add a test on the JsonSchema generation (tests/Functional/JsonSchema/JsonSchemaTest.php) as I'm not sure that we'd gather the correct properties.

Done in c2b0997 @soyuka

@Maxcastel Maxcastel marked this pull request as ready for review December 26, 2025 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants