You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To better understand where to go with #6288 I spent some time researching the history of (PHP 8.1+) enums in this library … i.e. what are the correct behavioural expectations, and any missing bits. So this issue is an attempt to bring together a bunch of context to discuss & find a clear path forward.
Right now the key questions are around how to handle \BackedEnums containing the ApiResource class attribute:
Have metadata generation remove operations; or
Add/fix support for serialization & schema generation (see example below)
Properties of type \BackedEnum should always normalize to the enum ->value not an iri
Schema properties of type \BackedEnum should always be {"enum": [value1, value2, value3, ...]} not the iri type
Enum resource routes return only name & value properties by default
cases should probably never be included as an enum item property
Automatic registration of item & collection GET providers
One use-case for the latter is where there is a need to provide enum metadata lookup, e.g. in the example below a human friendly description of a Status' integer.
I'm happy to try and cover json and jsonapi, and @soyuka is already working on jsonld … jsonhal is probably easy for me to fit in too if needed.
Finally after quite some time (this issue was opened in 2018!), we have support for Enums in OpenAPI, REST and GraphQL. Both Doctrine ORM and ODM have support for enums through enumType which is supported by API Platform 3.0. Alan added their support over OpenAPI and GraphQL.
How to expose Enums with API Platform — a blog post that got attention showing how to expose enums as resources. This is what I have based my assumptions/approach around here. One has to start somewhere.
To better understand where to go with #6288 I spent some time researching the history of (PHP 8.1+) enums in this library … i.e. what are the correct behavioural expectations, and any missing bits. So this issue is an attempt to bring together a bunch of context to discuss & find a clear path forward.
Right now the key questions are around how to handle
\BackedEnums containing theApiResourceclass attribute:\BackedEnumshould always normalize to the enum->valuenot an iriSchema properties of type\BackedEnumshould always be{"enum": [value1, value2, value3, ...]}not the iri typename&valueproperties by defaultcasesshould probably never be included as an enum item propertyGETprovidersOne use-case for the latter is where there is a need to provide enum metadata lookup, e.g. in the example below a human friendly description of a Status' integer.
I'm happy to try and cover
jsonandjsonapi, and @soyuka is already working onjsonld…jsonhalis probably easy for me to fit in too if needed.Background
@soyuka's blog post API Platform 3.1 is out!
How to expose Enums with API Platform — a blog post that got attention showing how to expose enums as resources. This is what I have based my assumptions/approach around here. One has to start somewhere.
Issues
Implementation
Misc
Related to Symfony Changes
PRs
Current State Examples
Note
For simplicity I'm just sticking with
Accept: application/jsonhere.Enum as Property Value
This works and both the schema and JSON response are as expected.
Model
Schema
{ "Article": { "type": "object", "description": "", "deprecated": false, "properties": { "id": { "readOnly": true, "type": "integer" }, "title": { "type": "string" }, "audit": { "type": "string", "enum": [ "pending", "passed", "failed" ] }, "status": { "type": "integer", "enum": [0, 1, 2] } } } }{ "id": 1, "title": "Once Upon A Title", "audit": "passed", "status": 1 }Enum Resource as Property Value
Model
Schema
{ "Article": { "type": "object", "description": "", "deprecated": false, "properties": { "id": { "readOnly": true, "type": "integer" }, "title": { "type": "string" }, "audit": { "type": "string", "format": "iri-reference", "example": "https://example.com/" }, "status": { "type": "string", "format": "iri-reference", "example": "https://example.com/" } } }, "Audit": { "type": "object", "description": "", "deprecated": false, "properties": { "name": { "readOnly": true, "type": "string" }, "value": { "readOnly": true, "allOf": [ { "type": "string" }, { "type": "integer" } ] }, "id": { "readOnly": true, "anyOf": [ { "type": "string" }, { "type": "integer" } ] }, "cases": { "readOnly": true } } }, "Status": { "See above": "Same as Audit" } }{ "id": 1, "title": "Once Upon A Title", "audit": "/audits/passed", "status": "/statuses/1" }This immediately breaks
Article::statusas it becomes an iri instead. Normal and correct for everything that's not an enum.… and the enum resource routes produce "interesting" responses …
Response
{ "name": "Pending", "value": "pending", "id": "pending", "cases": [ "/audits/pending", { "name": "Passed", "value": "passed", "id": "passed", "cases": [ "/audits/pending", "/audits/passed", { "name": "Failed", "value": "failed", "id": "failed", "cases": [ "/audits/pending", "/audits/passed", "/audits/failed" ] } ] }, { "name": "Failed", "value": "failed", "id": "failed", "cases": [ "/audits/pending", { "name": "Passed", "value": "passed", "id": "passed", "cases": [ "/audits/pending", "/audits/passed", "/audits/failed" ] }, "/audits/failed" ] } ] }Response
{ "name": "DRAFT", "value": 0, "description": "Article is not ready for public consumption", "id": 0, "cases": [ "/statuses/0", { "name": "PUBLISHED", "value": 1, "description": "Article is publicly available", "id": 1, "cases": [ "/statuses/0", "/statuses/1", { "name": "ARCHIVED", "value": 2, "description": "Article content is outdated or superseded", "id": 2, "cases": [ "/statuses/0", "/statuses/1", "/statuses/2" ] } ] }, { "name": "ARCHIVED", "value": 2, "description": "Article content is outdated or superseded", "id": 2, "cases": [ "/statuses/0", { "name": "PUBLISHED", "value": 1, "description": "Article is publicly available", "id": 1, "cases": [ "/statuses/0", "/statuses/1", "/statuses/2" ] }, "/statuses/2" ] } ] }