nelmio / NelmioApiDocBundle

Generates documentation for your REST API from annotations
MIT License
2.23k stars 836 forks source link

[Bug]: enum_exists(): Argument #1 ($enum) must be of type string, array given #2327

Closed DipenAVDEVS closed 1 week ago

DipenAVDEVS commented 3 months ago

Version

4.23

Description

Argument #1 ($enum) must be of type string, array given
TRACE: #0 /srv/www/vendor/nelmio/api-doc-bundle/src/ModelDescriber/JMSModelDescriber.php(297): enum_exists()
#1 /srv/www/vendor/nelmio/api-doc-bundle/src/ModelDescriber/JMSModelDescriber.php(180): Nelmio\\ApiDocBundle\\ModelDescriber\\JMSModelDescriber->describeItem()
#2 /srv/www/vendor/nelmio/api-doc-bundle/src/Model/ModelRegistry.php(96): Nelmio\\ApiDocBundle\\ModelDescriber\\JMSModelDescriber->describe()
#3 /srv/www/vendor/nelmio/api-doc-bundle/src/ApiDocGenerator.php(129): Nelmio\\ApiDocBundle\\Model\\ModelRegistry->registerSchemas()
#4 /srv/www/vendor/nelmio/api-doc-bundle/src/Render/RenderOpenApi.php(76): Nelmio\\ApiDocBundle\\ApiDocGenerator->generate()
#5 /srv/www/vendor/nelmio/api-doc-bundle/src/Render/RenderOpenApi.php(61): Nelmio\\ApiDocBundle\\Render\\RenderOpenApi->render()\n#6 /srv/www/vendor/nelmio/api-doc-bundle/src/

When i update version from 4.10 to 4.23 . i am getting above issue

JSON OpenApi

JSON OpenApi ```json Replace this text with your JSON (`bin/console nelmio:apidoc:dump`) ```

Additional context

nelmio_api_doc: models: use_jms: true

documentation:
    info:
        title: Medikarma API Documentation v1.0.10
        description: >
            ## Overview

            Every request requires headers: `Authorization` and `Content-Type`. These headers are required
            regardless of the method type (GET, POST, PUT, DELETE, PATCH) used.

            * The `Content-Type` describes the data format you are transmitting.

            * The `Authorization` header is where you supply the access token. Application use Bearer Authentication.
        version: 1.0.10
    servers:
        - url: '%env(API_SERVER_URL)%'
          description: API Server
    components:
        schemas:
            version:
                type: string
                enum:
                    - v1
            category:
                type: string
                enum:
                    - !php/const App\Entity\Class::test
                    - !php/const App\Entity\Class::test
            statisticsType:
                type: string
                enum:
                    - !php/const App\Entity\Enum\Class::test
                    - !php/const App\Entity\Enum\Class::test
                    - !php/const App\Entity\Enum\Class::test
            cholesterolType:
                type: string
                enum:
                    - !php/const App\Component\Chart\DTO\Class::test
                    - !php/const App\Component\Chart\DTO\Class::test
                    - !php/const App\Component\Chart\DTO\Class::test
            ClinicStatus:
                type: integer
                enum:
                    - !php/const App\Entity\Enum\Class::test
                    - !php/const App\Entity\Enum\Class::test
                description: >
                    Values:
                     * `0` - Inactive
                     * `1` - Active
            connectionType:
                type: integer
                enum:
                    - !php/const App\Entity\Enum\Class::test
                    - !php/const App\Entity\Enum\Class::test
                description: >
                    Values:
                     * `1` - Direct
                     * `2` - OneUpHealth
            glucoseProviderType:
                type: string
                enum:
                    - !php/const App\Entity\Enum\Class::test
                    - !php/const App\Entity\Enum\Class::test
            diagnosticReportType:
                type: string
                enum:
                    - !php/const App\Entity\Class::test
                    - !php/const App\Entity\Class::test
            dataCategory:
                type: string
                enum:
                    - !php/const App\Component\Patient\Class::test
                    - !php/const App\Component\Patient\Enum\Class::test
            ViewingStatus:
                type: integer
                enum:
                    - !php/const App\Entity\Class::test
                    - !php/const App\Entity\Class::test
                description: >
                    Values:
                     * `0` - Old
                     * `1` - New
            AppName:
                type: string
                enum:
                    - !php/const App\Entity\Class::test
                    - !php/const App\Entity\Class::test
            questionCategory:
                type: string
                enum:
                    - !php/const App\Entity\Class::test
            activityCategory:
                type: string
                enum:
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
            ScoreType:
                type: string
                enum:
                    - !php/const App\Entity\Class::test
                    - !php/const App\Entity\Class::test
            MedicationStatus:
                type: string
                enum:
                    - !php/const App\Entity\Class::test
                    - !php/const App\Entity\Class::test
            DietValue:
                type: integer
                enum:
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
                description: >
                    Values:
                     * `1` - Test1
                     * `2` - test2
                     * `3` - Test3
            ExerciseValue:
                type: integer
                enum:
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
                description: >
                    Values:
                     * `1` - LOW
                     * `2` - MEDIUM
                     * `3` - HIGH
            StressValue:
                type: integer
                enum:
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
                description: >
                    Values:
                     * `1` - LOW
                     * `2` - FAIR
                     * `3` - HIGH
            MoodValue:
                type: integer
                enum:
                    - !php/const App\Component\Class::test
                    - !php/const App\Component\Class::test
                description: >
                    Values:
                     * `1` - Test1
                     * `2` - Test2
            BadRequestResponse:
                type: object
                properties:
                    code:
                        type: integer
                        example: 400
                    message:
                        type: string
                        example: 'Validation Failed'
                    errors:
                        type: array
                        items:
                            type: string
                            example: "Value shouldn't be blank."
            MessageTypeEnum:
                type: string
                enum:
                    - !php/const App\Entity\Class::test
                    - !php/const App\Entity\Class::test
        securitySchemes:
            Bearer:
                type: apiKey
                description: 'Value: Bearer {jwt}'
                name: Authorization
                in: header
        parameters:
            version:
                name: version
                in: path
                required: true
                schema:
                    $ref: '#/components/schemas/version'
    security:
        - Bearer: []
areas: # to filter documented areas
    default:
        path_patterns:
            - ^/api(?!/tech/(.*)|/public/terra/(.*)|/graphql/(.*)|/doc(.*)|/admin(.*)$) # Accepts routes under /api except /api/doc and api/tech
            - ^/_qa
            - ^/auth
        disable_default_routes: true

image_2024_07_26T05_39_44_106Z

DipenAVDEVS commented 3 months ago

Still facing same issue . i try with sudo composer update and its update version to 4.23.1(nelmio/api-doc-bundle) but then also its show same error image

DjordyKoert commented 3 months ago

Do you also have this issue with the latest version?

DipenAVDEVS commented 3 months ago

Yes , when i try with composer update its update to 4.23 and show me same error.

bobvandevijver commented 1 month ago

I'm seeing this when I override the type with JMS to enforce a backed enum to be serialized with its name. This looks as follows:

#[Type('enum<' . ActiveCampaignListStatusEnum::class .', name>')]

This is caused due to the type not being just a string, but an array:

https://github.com/nelmio/NelmioApiDocBundle/blame/9b91d2376888c2dacf4192b103a7ff7f67112bf5/src/ModelDescriber/JMSModelDescriber.php#L331

So, it looks like this:

image

As that comes from JMS and is handled in JMS correctly, I believe it should be handled here as well. PR incoming!

bobvandevijver commented 1 month ago

It is only partly handled in JMS, because for this particular type annotation you apparently are required to wrap the type with quotes. So, for the example above it should become:

#[Type('enum<\'' . ActiveCampaignListStatusEnum::class .'\', 'name'>')]
or
#[Type("enum<'". ActiveCampaignListStatusEnum::class . "', 'name'>')]

Then it works as it should. Another PR will be made at JMS to solve this inconsistency.