SoliDry / api-generator

PHP-code generator for Laravel framework, with complete support of JSON-API data format
MIT License
328 stars 60 forks source link

Class 'Modules\V3\Http\Requests\TagFormRequest' not found #187

Closed ghosttk closed 4 years ago

ghosttk commented 4 years ago

Installing from composer, there's no "test" directory included. I copy the openapi.yaml and topic.yaml from github to my root directory, then run "./artisan api:generate openapi.yaml --migrations', and recieve these errors:

Symfony\Component\Debug\Exception\FatalThrowableError : Class 'Modules\V3\Http\Requests\TagFormRequest' not found

` at /home/ww/projects/php/api-gen/vendor/solidry/api-generator/src/Blocks/Entities.php:159 155| public function createPivot() 156| { 157| $formRequestEntity = $this->getFormRequestEntity($this->generator->version, $this->className); 158| /* @var BaseFormRequest $formRequest */

159| $formRequest = new $formRequestEntity(); 160| if (method_exists($formRequest, ModelsInterface::MODEL_METHOD_RELATIONS)) { 161| $relations = $formRequest->relations(); 162| $this->sourceCode .= PHP_EOL; // margin top from props 163| foreach ($relations as $relationEntity) {

Exception trace:

1 SoliDry\Blocks\Entities::createPivot() /home/ww/projects/php/api-gen/vendor/solidry/api-generator/src/Controllers/GeneratorTrait.php:133

2 SoliDry\Controllers\BaseCommand::solveEntities() /home/ww/projects/php/api-gen/vendor/solidry/api-generator/src/Controllers/GeneratorTrait.php:53

Please use the argument -v to see more details. `

arthurkushman commented 4 years ago

Hi, there shouldn't be test directory because of .gitattributes file exclusion rules that prevents downloading files needless on production - it is a best practice.

About an error - have u changed those files? Can u provide links where have u been getting those? Do they look like this? openapi.yaml:

openapi: 3.0.1
info:
  title: Articles
  description: This api provides access to articles
  version: v3
servers:
- url: https://{environment}.example.com:{port}/{basePath}
  description: Production server
  variables:
    environment:
      default: api
      description: An api for devices at Google dot com
    port:
      enum:
        - 80
        - 443
      defualt: 80
    basePath:
      default: v3
# to declare globally which files to include with other component declarations
uses:
  topics: oas/topic.yaml
components:
  schemas:
    ID:
      type: integer
      required: true
      maximum: 20
    SID:
      type: string
      required: true
      maxLength: 128
    Type:
      type: string
      required: true
      minLength: 1
      maxLength: 255
    DataObject:
      type: object
      required: true
    DataArray:
      type: array
      required: true
    RelationshipsDataItem:
      type: object
      properties:
        id: ID
        type: Type
    RelationshipsDataItemSID:
      type: object
      properties:
        id: SID
        type: Type
    Redis:
      type: object
    TagRelationships:
      description: Tag relationship description
      type: object
      properties:
        data:
          type: DataArray
          items:
            type: RelationshipsDataItem
    TagAttributes:
      description: Always set attributes for object
      type: object
      properties:
        title:
          type: string
          required: true
          minLength: 3
    Tag:
      type: object
      properties:
        type: Type
        id: ID
        attributes: TagAttributes
        relationships:
          type: ArticleRelationships[]
        cache:
          type: Redis
          properties:
            stampede_xfetch:
              type: boolean
              default: true
            stampede_beta:
              type: number
              default: 1.1
            ttl:
              type: integer
              default: 3600
    ArticleRelationships:
      type: object
      properties:
        data:
          type: DataArray
          items:
            type: RelationshipsDataItem
    ArticleAttributes:
      description: Article attributes description
      type: object
      properties:
        title:
          required: true
          type: string
          minLength: 16
          maxLength: 256
          facets:
            index:
              idx_title: index
        description:
          required: true
          type: string
          minLength: 32
          maxLength: 1024
          facets:
            spell_check: true
            spell_language: en
        url:
          required: false
          type: string
          minLength: 16
          maxLength: 255
          facets:
            index:
              idx_url: unique
        show_in_top:
          description: Show at the top of main page
          required: false
          type: boolean
        status:
          description: The state of an article
          enum: ["draft", "published", "postponed", "archived"]
          facets:
            state_machine:
              initial: ['draft']
              draft: ['published']
              published: ['archived', 'postponed']
              postponed: ['published', 'archived']
              archived: []
        topic_id:
          description: ManyToOne Topic relationship
          required: true
          type: integer
          minimum: 1
          maximum: 6
          facets:
            index:
              idx_fk_topic_id: foreign
              references: id
              on: topic
              onDelete: cascade
              onUpdate: cascade
        rate:
          type: number
          minimum: 3
          maximum: 9
          format: double
        date_posted:
          type: date-only
        time_to_live:
          type: time-only
        deleted_at:
          type: datetime
    Article:
      type: object
      properties:
        type: Type
        id: SID
        attributes: ArticleAttributes
        relationships:
          type: TagRelationships[] | TopicRelationships
        cache:
          type: Redis
          properties:
            stampede_xfetch:
              type: boolean
              default: true
            stampede_beta:
              type: number
              default: 1.5
            ttl:
              type: integer
              default: 300
    TopicRelationships:
      type: object
      properties:
        data:
          type: DataArray
          items:
            type: RelationshipsDataItem
    QueryParams:
      type: object
      properties:
        page:
          type: integer
          required: false
          description: page number
        limit:
          type: integer
          required: false
          default: 15
          example: 15
          description: elements per page
        sort:
          type: string
          required: false
          pattern: "asc|desc"
          default: "desc"
        access_token:
          type: string
          required: true
          example: db7329d5a3f381875ea6ce7e28fe1ea536d0acaf
          description: sha1 example
          default: db7329d5a3f381875ea6ce7e28fe1ea536d0acaf
    UserAttributes:
      type: object
      properties:
        first_name:
          required: true
          type: string
          minLength: 16
          maxLength: 256
        last_name:
          required: false
          type: string
          minLength: 16
          maxLength: 256
          facets:
            composite_index:
              index: ['first_name', 'last_name'] # can be unique, primary
        password:
          description: user password to refresh JWT (encrypted with password_hash)
          required: true
          type: string
          maxLength: 255
        jwt:
          description: Special field to run JWT Auth via requests
          required: true
          type: string
          maxLength: 512
          default: ' '
        permissions:
          type: integer
          required: false
          maximum: 20
          facets:
            bit_mask:
              publisher: 1
              editor: 2
              manager: 4
              photo_reporter: 8
              admin: 16
    User:
      type: object
      properties:
        type: Type
        id: ID
        attributes: UserAttributes
    Trees:
      type: object
      properties:
        menu:
          type: boolean
          default: true
    MenuAttributes:
      type: object
      properties:
        title:
          required: true
          type: string
        rfc:
          type: string
          default: /
        parent_id:
          description: mandatory field for building trees
          type: integer
          maximum: 10
          default: 0
    Menu:
      type: object
      properties:
        type: Type
        id: ID
        attributes: MenuAttributes
/article:
  get:
    queryParameters: QueryParams
    responses:
      200:
        body:
          application/json.vnd+json:
            type: Article[]
  post:
    body:
      application/json.vnd+json:
        type: Article
    responses:
      200:
        body:
          application/json.vnd+json:
            type: Article
  /{id}:
    get:
      responses:
        200:
          body:
            application/json.vnd+json:
              type: Article
    patch:
      body:
        application/json.vnd+json:
          type: Article
      responses:
        200:
          body:
            application/json.vnd+json:
              type: Article
    delete:
      responses:
        204:
    /relationships/tag:
      get:
        responses:
          200:
            body:
              application/json.vnd+json:
                type: TagRelationships
      post:
        body:
          application/json.vnd+json:
            type: TagRelationships
        responses:
          201:
            body:
              application/json.vnd+json:
                type: TagRelationships
      patch:
        body:
          application/json.vnd+json:
            type: TagRelationships
        responses:
          200:
            body:
              application/json.vnd+json:
                type: TagRelationships
      delete:
        responses:
          204:
    /tag:
      get:
        responses:
          200:
            body:
              application/json.vnd+json:
                type: Tag[]

and topic.yaml:

openapi: 3.0.1
info: This api provides access to articles
servers:
- url: https://{environment}.example.com:{port}/{basePath}
  description: Production server
  variables:
    environment:
      default: api
      description: An api for devices at Google dot com
    port:
      enum:
        - 80
        - 443
      defualt: 80
    basePath:
      default: v3
components:
  schemas:
    ID:
      type: integer
      required: true
    Type:
      type: string
      required: true
      minLength: 1
      maxLength: 255
    DataObject:
      type: object
      required: true
    DataArray:
      type: array
      required: true
    RelationshipsDataItem:
      type: object
      properties:
        id: ID
        type: Type
    ArticleRelationships:
      type: object
      properties:
        data:
          type: DataArray
          items:
            type: RelationshipsDataItem
    TopicAttributes:
      type: object
      properties:
        title:
          required: true
          type: string
          minLength: 16
          maxLength: 256
    Topic:
      type: object
      properties:
        type: Type
        id: ID
        attributes: TopicAttributes
        relationships:
          type: ArticleRelationships[]

There must be both entities TagAttributes and Tag - otherwise it wont be generated.

arthurkushman commented 4 years ago

Just in case generated result should be like this: Screen Shot 2020-01-15 at 09 31 30

ghosttk commented 4 years ago

I got these two files from https://github.com/SoliDry/api-generator/tree/master/tests, which were just the same as you provided, and I didn't change them. The generated 'Http' directory only contained a few files.

Modules/V3/Http/ ├── Controllers │   ├── DefaultController.php │   ├── TagController.php │   └── V3Controller.php ├── Middleware └── Requests └── TagFormRequest.php

3 directories, 4 files

Below is the full command output: ` Created : /home/ww/projects/php/api-gen/Modules/V3/module.json Created : /home/ww/projects/php/api-gen/Modules/V3/Routes/web.php Created : /home/ww/projects/php/api-gen/Modules/V3/Routes/api.php Created : /home/ww/projects/php/api-gen/Modules/V3/Resources/views/index.blade.php Created : /home/ww/projects/php/api-gen/Modules/V3/Resources/views/layouts/master.blade.php Created : /home/ww/projects/php/api-gen/Modules/V3/Config/config.php Created : /home/ww/projects/php/api-gen/Modules/V3/composer.json Created : /home/ww/projects/php/api-gen/Modules/V3/Resources/assets/js/app.js Created : /home/ww/projects/php/api-gen/Modules/V3/Resources/assets/sass/app.scss Created : /home/ww/projects/php/api-gen/Modules/V3/webpack.mix.js Created : /home/ww/projects/php/api-gen/Modules/V3/package.json Created : /home/ww/projects/php/api-gen/Modules/V3/Database/Seeders/V3DatabaseSeeder.php Created : /home/ww/projects/php/api-gen/Modules/V3/Providers/V3ServiceProvider.php Created : /home/ww/projects/php/api-gen/Modules/V3/Providers/RouteServiceProvider.php Created : /home/ww/projects/php/api-gen/Modules/V3/Http/Controllers/V3Controller.php Module [V3] created successfully.

In ProviderRepository.php line 208:

Class 'Modules\V3\Providers\V3ServiceProvider' not found

In ProviderRepository.php line 208:

Class 'Modules\V3\Providers\V3ServiceProvider' not found

Modules/V3/Config/config.php created =============== Tag Entities Modules/V3/Http/Controllers/DefaultController.php created Modules/V3/Http/Controllers/TagController.php created Modules/V3/Http/Requests/TagFormRequest.php created

Symfony\Component\Debug\Exception\FatalThrowableError : Class 'Modules\V3\Http\Requests\TagFormRequest' not found

at /home/ww/projects/php/api-gen/vendor/solidry/api-generator/src/Blocks/Entities.php:159 155| public function createPivot() 156| { 157| $formRequestEntity = $this->getFormRequestEntity($this->generator->version, $this->className); 158| /* @var BaseFormRequest $formRequest */

159| $formRequest = new $formRequestEntity(); 160| if (method_exists($formRequest, ModelsInterface::MODEL_METHOD_RELATIONS)) { 161| $relations = $formRequest->relations(); 162| $this->sourceCode .= PHP_EOL; // margin top from props 163| foreach ($relations as $relationEntity) {

Exception trace:

1 SoliDry\Blocks\Entities::createPivot() /home/ww/projects/php/api-gen/vendor/solidry/api-generator/src/Controllers/GeneratorTrait.php:133

2 SoliDry\Controllers\BaseCommand::solveEntities() /home/ww/projects/php/api-gen/vendor/solidry/api-generator/src/Controllers/GeneratorTrait.php:53

`

arthurkushman commented 4 years ago

Actually don't see the root cause yet, but did the sequence of actions to check:

  1. Run composer update (just in case I don`t have V3 directory in Modules at all at this moment to generate it later)
    ✗ composer update solidry/api-generator
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Nothing to install or update
    Generating optimized autoload files
    > Illuminate\Foundation\ComposerScripts::postAutoloadDump
    > @php artisan package:discover --ansi
    Discovered Package: beyondcode/laravel-dump-server
    Discovered Package: fideloper/proxy
    Discovered Package: laravel/tinker
    Discovered Package: nesbot/carbon
    Discovered Package: nunomaduro/collision
    Discovered Package: nwidart/laravel-modules
    Discovered Package: solidry/api-generator
    Package manifest generated successfully.
  2. Run generator command (try to run it with --regenerate option)
    php artisan api:generate oas/openapi.yaml --migrations --regenerate
    Created : /Users/arthur/www/laravel-api/Modules/V3/module.json
    Created : /Users/arthur/www/laravel-api/Modules/V3/Routes/web.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/Routes/api.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/Resources/views/index.blade.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/Resources/views/layouts/master.blade.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/Config/config.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/composer.json
    Created : /Users/arthur/www/laravel-api/Modules/V3/Resources/assets/js/app.js
    Created : /Users/arthur/www/laravel-api/Modules/V3/Resources/assets/sass/app.scss
    Created : /Users/arthur/www/laravel-api/Modules/V3/webpack.mix.js
    Created : /Users/arthur/www/laravel-api/Modules/V3/package.json
    Created : /Users/arthur/www/laravel-api/Modules/V3/Database/Seeders/V3DatabaseSeeder.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/Providers/V3ServiceProvider.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/Providers/RouteServiceProvider.php
    Created : /Users/arthur/www/laravel-api/Modules/V3/Http/Controllers/V3Controller.php
    Module [V3] created successfully.
    Module [V3] used successfully.
    +------+----------+-------+------------------------------------------+
    | Name | Status   | Order | Path                                     |
    +------+----------+-------+------------------------------------------+
    | V1   | Disabled | 0     | /Users/arthur/www/laravel-api/Modules/V1 |
    | V2   | Disabled | 0     | /Users/arthur/www/laravel-api/Modules/V2 |
    | V3   | Enabled  | 0     | /Users/arthur/www/laravel-api/Modules/V3 |
    +------+----------+-------+------------------------------------------+
    Modules/V3/Config/config.php created
    =============== Tag Entities
    Modules/V3/Http/Controllers/DefaultController.php created
    Modules/V3/Http/Controllers/TagController.php created
    Modules/V3/Http/Requests/TagFormRequest.php created
    Modules/V3/Entities/TagArticle.php created
    Modules/V3/Entities/Tag.php created
    Modules/V3/Routes/api.php created
    tests/functional/TagCest.php created
    Modules/V3/Database/Migrations/2020_01_16_092810_create_tag_table.php created
    Modules/V3/Database/Migrations/2020_01_16_092813_create_tag_article_table.php created
    =============== Article Entities
    Modules/V3/Http/Controllers/ArticleController.php created
    Modules/V3/Http/Requests/ArticleFormRequest.php created
    Modules/V3/Entities/Article.php created
    tests/functional/ArticleCest.php created
    Modules/V3/Database/Migrations/2020_01_16_092812_create_article_table.php created
    =============== User Entities
    Modules/V3/Http/Controllers/UserController.php created
    Modules/V3/Http/Requests/UserFormRequest.php created
    Modules/V3/Entities/User.php created
    tests/functional/UserCest.php created
    Modules/V3/Database/Migrations/2020_01_16_092814_create_user_table.php created
    =============== Menu Entities
    Modules/V3/Http/Controllers/MenuController.php created
    Modules/V3/Http/Requests/MenuFormRequest.php created
    Modules/V3/Entities/Menu.php created
    tests/functional/MenuCest.php created
    Modules/V3/Database/Migrations/2020_01_16_092816_create_menu_table.php created
    =============== Topic Entities
    Modules/V3/Http/Controllers/TopicController.php created
    Modules/V3/Http/Requests/TopicFormRequest.php created
    Modules/V3/Entities/Topic.php created
    tests/functional/TopicCest.php created
    Modules/V3/Database/Migrations/2020_01_16_092818_create_topic_table.php created
  3. Check the structure of Dirs/Classes - all in place.

PS may be some of yaml/json files corrupted? Can u attach those files - to let me try with them? It seems like everything works good on my side. There must be definitely more classes generated in Controllers/Requests/Entities for Tag, Article, Menu, Topic, User entities than what u got as a result.

ghosttk commented 4 years ago

After running composer update solidry/api-generator, everything works as expected.

- Updating solidry/api-generator (2.6.8 => 2.6.9): Downloading (100%) The cause is versions. First installed version via composer days before is 2.6.8. Thank you!