-
Notifications
You must be signed in to change notification settings - Fork 937
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
Allow attributes' enums to be specified with an enum class string #1303
Allow attributes' enums to be specified with an enum class string #1303
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks quite good and the fact that there is a test makes it even better :)
The only thing I'd prefer done differently is the use of the Util
class.
I've been trying to move away from using it and/or adding to it. There is a lot of logic spread across a lot of files and in additrion I would suspect that this doesn't work for annotations?
As an alternative the convertEnum
code should be added to the ExpandEnums
processor which would cover all cases in a single place. Looks like $enum
is coming from either Schema
or ServerVariable
, so adding some new processing loop like this should deal with all annotations/attributes:
/** @var AnnotationSchema[] $schemas */
$schemas = $analysis->getAnnotationsOfType([AnnotationSchema::class, AttributeSchema::class, ServerVariable::class]);
foreach ($schemas as $schema) {
}
}
Thank you.
Yes. But it would be nice if it could be used for annotations as well.
I am very happy to learn that there is an expansion point! I understood the source line to be as follows I haven't caught up with understanding the source yet, but is it like rewriting the context enum value in the above loop? I have one question. https://github.com/zircote/swagger-php/blob/master/src/Context.php#L28 |
Yes, you'd just need a separate loop in order to catch also
The context |
tests/UtilTest.php
Outdated
public function convertEnumsFixtures(): iterable | ||
{ | ||
return [ | ||
[\OpenApi\Tests\Fixtures\PHP\StatusEnumBacked::class, [1, 2, 3]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please use use
statements and import those enums rather than using FQDN?
I have been following the implementation and feel that it can be handled with the following description. #[Schema(description: "backend status", example: StatusEnumBacked::DRAFT)]
enum StatusEnumBacked: int
{
case DRAFT = 1;
case PUBLISHED = 2;
case ARCHIVED = 3;
} #[Property('status', refs: '#/components/schemas/StatusEnumBacked')]
public StatusEnumBacked status; Is it universal usage to refer to it by ref as in this code? The only problem with the way I wrote this is that I could not overwrite the nullable value. #[Property('status', refs: '#/components/schemas/StatusEnumBacked', nullable: true)]
public ?StatusEnumBacked status; Would it be preferable to eliminate this problem? |
Yes, that is an known issue that is not easy to solve.
Not as they are. The 3.0.0 spec does not allow any other keys next to
Time and money :) Still waiting for the updates to the PR as requested. |
On the topic of using <?php
use OpenApi\Attributes as OAT;
#[OAT\Schema(description: "backend status", example: StatusEnumBacked::DRAFT)]
enum StatusEnumBacked: int
{
case DRAFT = 1;
case PUBLISHED = 2;
case ARCHIVED = 3;
}
#[OAT\Schema]
class Model
{
#[OAT\Property(nullable: true)]
public ?StatusEnumBacked $status;
} will generate this YAML:
|
I understood that the OpenAPI specification makes it difficult to achieve this in the first place. Shall we resume consideration when we have more time and money.. 😅
I understand that this modification will be an effective way to define nullable enums, so I would like to proceed. |
Should I separate the Processor class( With a class name like I am a little unsure if it will work with the Processor dependencies, but I am thinking of running |
Nah, just leave it all in the same processor, just split into two separate methods. |
I took your advice and modified ExpandEnums. I haven't been able to implement the test, but is this what it looks like? |
Tests have been implemented. |
9368411
to
3299a95
Compare
you can run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some nitpicking but good enough to merge :)
Thanks @k2tzumi |
Proposal
When using Attribute to describe specifications, we would like to synchronize the enum property with the Enum class.
I thought it would be a good idea to have the enum value automatically expanded from the Enum class name.
What we dare not do
Allowing
UnitEnum
arrays to be specified.In PHP8.1, IDE and linter warn when calling
EnumClass::cases()
in Attribute, so it was decided not to support it at this time.For example, here are some warnings
Constant expression contains invalid operations
Cannot call abstract static method UnitEnum::cases().
Could be a problem with the PHP version, but do you have any knowledge of this issue?