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

Not generating reusable Schemas #28

Open
carlos-algms opened this issue Nov 3, 2024 · 1 comment
Open

Not generating reusable Schemas #28

carlos-algms opened this issue Nov 3, 2024 · 1 comment

Comments

@carlos-algms
Copy link

When a Schema is created inside of the ResponseFactory it's in included in the generated document.
However, when a a Schema is created and implements Reusable, generated document adds a reference to it, but doesn't generate the actual code.

Code

// ValidResponse.php
use GoldSpecDigital\ObjectOrientedOAS\Objects\MediaType;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Response;
use Vyuldashev\LaravelOpenApi\Factories\ResponseFactory;
use App\Modules\Clients\OpenApi\Schemas\ClientSchema;

class ValidResponse extends ResponseFactory {
  public function build(): Response {
    return Response::ok()
      ->description('User is logged and cookie was decrypted')
      ->content(MediaType::json()->schema(ClientSchema::ref()));
  }
}

// ClientSchema.php
use GoldSpecDigital\ObjectOrientedOAS\Contracts\SchemaContract;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use Vyuldashev\LaravelOpenApi\Contracts\Reusable;
use Vyuldashev\LaravelOpenApi\Factories\SchemaFactory;

class ClientSchema extends SchemaFactory implements Reusable {
  /**
   * @return Schema
   */
  public function build(): SchemaContract {
    return Schema::object('Client')->properties(
      Schema::string('id')->default(0),
      Schema::string('name')->default('NULL'),
    );
  }
}

// ClientController.php
#[OpenApi\PathItem]
class ClientController extends Controller {
  /**
   * Get the current authenticated user.
   */
  #[OpenApi\Operation]
  #[
    OpenApi\Response(
      factory: ValidResponse::class,
      statusCode: HTTPResponse::HTTP_OK,
    ),
  ]
  public function me(Request $request): Client {
    return $request->user();
  }
}

Result

{
  "openapi": "3.0.2",
  "info": {
    "title": "My API",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://localhost"
    }
  ],
  "paths": {
    "/users/me": {
      "get": {
        "summary": "Get the current authenticated user.",
        "responses": {
          "204": {
            "description": "User is not authenticated or invalid cookie"
          },
          "200": {
            "description": "User is logged",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Client"
                }
              }
            }
          }
        }
      }
    }
  }
}
@TartanLeGrand
Copy link
Owner

Hello 👋,

Can you add more informations. Version of the package ?

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

No branches or pull requests

2 participants