Skip to content
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

Add enum support #1050

Merged
merged 3 commits into from
Jan 9, 2022
Merged

Add enum support #1050

merged 3 commits into from
Jan 9, 2022

Conversation

DerManoMann
Copy link
Collaborator

Fixes #1043

@ymilin
Copy link

ymilin commented Jan 4, 2022

Hi,

I downloaded and tried this PR, the case we discussed in the issue works really well 👍 :

#[Schema]
enum Status
{
    case DRAFT;
    case PUBLISHED;
    case ARCHIVED;
}

Successfully generates:

components:
  schemas:
    Status:
      type: string
      enum:
        - DRAFT
        - PUBLISHED
        - ARCHIVED

I then tried to use it in a Schema like this:

#[Schema]
class BlogPost {
    public function __construct(
        #[Property] public int $id,
        #[Property] public string $content,
        #[Property] public Status $status,
    ) {}
}

which generates:

components:
  schemas:
    BlogPost:
      properties:
        id:
          type: integer
        content:
          type: string
        status: {  }         # <-- should be $ref: '#/components/schemas/Status'
      type: object
    Status:
      type: string
      enum:
        - DRAFT
        - PUBLISHED
        - ARCHIVED

Here $status in BlogPost should be inferred as a reference to #/components/schemas/Status

Manually setting ref works:

class BlogPost {
    public function __construct(
        #[Property] public int $id,
        #[Property] public string $content,
        #[Property(ref: "#/components/schemas/Status")] public Status $status, # <-- works
    ) {}
}

In a similar manner, I tried doing this:

enum Fizz {
    case FOO;
    case BAR;
    case BAZ;
}

#[Schema]
class Buzz {
    public function __construct(
        #[Property] public int $id,
        #[Property] public Fizz $fizz,
    ) {}
}

Which Generates:

components:
  schemas:
    Buzz:
      properties:
        id:
          type: integer
        fizz: {  }
      type: object

Should Generate:

components:
  schemas:
    Buzz:
      properties:
        id:
          type: integer
        fizz:
          type:string
          enum:
            - FOO
            - BAR
            - BAZ
      type: object

@DerManoMann DerManoMann merged commit 03ebfa7 into zircote:master Jan 9, 2022
@DerManoMann DerManoMann deleted the enums branch January 9, 2022 20:07
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.

Enums
2 participants