core icon indicating copy to clipboard operation
core copied to clipboard

Return the data type "string" for the enum PHP class in the JSON Schema document.

Open ymaillard opened this issue 3 years ago • 7 comments

Q A
Branch? 2.7
License MIT

Actually, the JSON Schema document specifies the data type "object" for an enum property. According to my interpretation of the specification, it should be a string ( https://swagger.io/docs/specification/data-models/enums/ )

This PR add a test if the class is a BackedEnum class and in this case, return an array ['type'=>'string, 'format'=>'enum']

ymaillard avatar Aug 02 '22 08:08 ymaillard

I don't think the enum format exists though.

alanpoulain avatar Aug 02 '22 08:08 alanpoulain

I don't think the enum format exists though.

I read on this page https://swagger.io/docs/specification/data-models/data-types/, 'format' is an open value. By example, I assume 'uuid' format used in the same method is not a value defined by open api specification. So enum format is a suggest.

ymaillard avatar Aug 02 '22 09:08 ymaillard

uuid is a built-in format, see https://json-schema.org/draft/2020-12/json-schema-validation.html#name-resource-identifiers. I don't think adding a custom format is a good idea, even if the specification allows it, since it will not be understood by clients.

alanpoulain avatar Aug 02 '22 09:08 alanpoulain

Ok I understand. Thanks for json-schema reference. I change my PR by removing the 'format' attribute. Is it ok for you ?

ymaillard avatar Aug 02 '22 10:08 ymaillard

Yes, also you should add a unit test.

alanpoulain avatar Aug 02 '22 10:08 alanpoulain

A Backed Enum may be backed by types of int or string, and a given enumeration supports only a single type at a time. Is there a way to detect the right type? Plus, some detail have to be taken in consideration. Please have a look at https://github.com/api-platform/core/issues/2254

GregoireHebert avatar Aug 02 '22 21:08 GregoireHebert

https://3v4l.org/dLRMX#v8.1.8 is an approach to find the backed type and associate it accordingly, or fall back into embedded object or resource as proposed in https://github.com/api-platform/core/issues/2254 I wonder how much should part this type detection should be part of PropertyInfo

GregoireHebert avatar Aug 02 '22 21:08 GregoireHebert

This can be added as well for GraphQl with https://github.com/OLYBe/core/commit/1b63666a7394dcf78ac30f11964d71bbf51bd8ac

webda2l avatar Sep 23 '22 19:09 webda2l

Superseded https://github.com/api-platform/core/pull/5120.

alanpoulain avatar Nov 04 '22 17:11 alanpoulain