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

Request DTO type Double #502

Closed
reinaldoborin opened this issue Jan 5, 2023 · 19 comments
Closed

Request DTO type Double #502

reinaldoborin opened this issue Jan 5, 2023 · 19 comments
Labels
question Further information is requested wontfix This will not be worked on

Comments

@reinaldoborin
Copy link

Describe the bug
I cant create a DTO like this:
https://github.com/cnizzardini/cakephp-swagger-bake-demo/blob/1.next/src/Dto/QueryDto.php

with a property like:
* @swag\SwagDtoQuery(name="Value", type="number", format="double", description="Required.")

To Reproduce
Steps to reproduce the behavior:

  1. Open and download Demo for version 1.
  2. Try to add a parameter type double on DTO.

Expected behavior
Work with number format types.

Version and Platform (please complete the following information):

  • OS/Platform: Ubuntu 18.05
  • CakePHP: 4.2
  • SwaggerBake Version: 1.7
@cnizzardini
Copy link
Owner

Do you get an error? What happens?

@reinaldoborin
Copy link
Author

reinaldoborin commented Jan 5, 2023

I forgot the full line, sorry.

/**
 * @Swag\SwagDtoQuery(name="Value", type="number", format="double", description="Required.")
 * @var number
 */
private $value;

/**
 * @return mixed
 */
public function getValue()
{
    return $this->value;
}

/**
 * @param mixed $value
 * @return QueryDto
 */
public function setValue($value)
{
    $this->value = $value;
    return $this;
}

Argument 1 passed to SwaggerBake\Lib\OpenApi\SchemaProperty::setType() must be of the type string, null given, called in /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/DtoParser.php on line 113

Only work with Integer or String.

@reinaldoborin
Copy link
Author

If i change to string or integer, works well, but i need the param to be a float or decimal.

@cnizzardini
Copy link
Owner

Odd behavior. What type of route is this? HTTP GET or HTTP POST/PUT/PATCH?

@cnizzardini
Copy link
Owner

Also, please post full stack trace.

@reinaldoborin
Copy link
Author

reinaldoborin commented Jan 5, 2023

2023-01-05 15:30:50 [TypeError] Argument 1 passed to SwaggerBake\Lib\OpenApi\SchemaProperty::setType() must be of the type string, null given, called in /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/DtoParser.php on line 113 in /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/OpenApi/SchemaTrait.php on line 45 Stack Trace: - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/DtoParser.php:113 - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationRequestBody.php:240 - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationRequestBody.php:93 - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php:89 - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Swagger.php:337 - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Swagger.php:81 - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Factory/SwaggerFactory.php:51 - /src/WebApp/vendor/cnizzardini/cakephp-swagger-bake/src/Controller/Component/SwaggerUiComponent.php:50 - /src/WebApp/vendor/cakephp/cakephp/src/Event/EventManager.php:309 - /src/WebApp/vendor/cakephp/cakephp/src/Event/EventManager.php:286 - /src/WebApp/vendor/cakephp/cakephp/src/Event/EventDispatcherTrait.php:92 - /src/WebApp/vendor/cakephp/cakephp/src/Controller/Controller.php:575 - /src/WebApp/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:96 - /src/WebApp/vendor/cakephp/cakephp/src/Http/BaseApplication.php:313 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:77 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Middleware/CsrfProtectionMiddleware.php:169 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/src/Middleware/DebitoEmAnaliseMiddleware.php:19 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/src/Middleware/UsuarioMiddleware.php:20 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/src/Middleware/TermosFranquiaMiddleware.php:18 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/src/Middleware/CorsMiddleware.php:18 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php:124 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Middleware/BodyParserMiddleware.php:159 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php:161 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php:68 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php:126 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php:60 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:73 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Runner.php:58 - /src/WebApp/vendor/cakephp/cakephp/src/Http/Server.php:90 - /src/WebApp/webroot/index.php:40 Request URL: /api/swagger Client IP: 127.0.0.1

Its a HTTP POST.

@cnizzardini
Copy link
Owner

cnizzardini commented Jan 5, 2023

I figured you'd say HTTP POST. The code was never designed expecting an HTTP GET based DTO to be used on a POST route and as such is failing because there is no condition for that.

Best practice is not to placing GET parameters on POST routes since you can just put everything in the POST body, but if you must my suggestion is just to use @SwagQuery on the controller action to document your GET parameters.

@cnizzardini
Copy link
Owner

Actually that won't work either (most likely) because that too expects GET parameters to only be used on HTTP GET routes....

@reinaldoborin
Copy link
Author

Sorry, if I didnt understood.

So the plugin doenst generete an endpoint (post) on swagger with body where can be double/decimal?

I already have the controller, the decimal works fine, but on swagger shows wrong (string or integer).

@cnizzardini
Copy link
Owner

If the route is HTTP POST, why not use @SwagDtoRequestBody?

@reinaldoborin
Copy link
Author

reinaldoborin commented Jan 5, 2023

Changed to

@swag\SwagRequestBody and @SwagDtoRequestBody(

Same problem.

@cnizzardini
Copy link
Owner

post stack trace please.

@reinaldoborin
Copy link
Author

@swag\SwagRequestBody

ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/OpenApi/SchemaTrait.php:45
SwaggerBake\Lib\OpenApi\SchemaProperty->setType
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/DtoParser.php:113
SwaggerBake\Lib\Operation\DtoParser->getSchemaProperties
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationRequestBody.php:240
SwaggerBake\Lib\Operation\OperationRequestBody->assignSwagDto
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationRequestBody.php:93
SwaggerBake\Lib\Operation\OperationRequestBody->getOperationWithRequestBody
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php:89
SwaggerBake\Lib\Operation\OperationFromRouteFactory->create
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Swagger.php:337
SwaggerBake\Lib\Swagger->buildPathsFromRoutes
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Swagger.php:81
SwaggerBake\Lib\Swagger->__construct
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Factory/SwaggerFactory.php:51
SwaggerBake\Lib\Factory\SwaggerFactory->create
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Controller/Component/SwaggerUiComponent.php:50
SwaggerBake\Controller\Component\SwaggerUiComponent->beforeFilter
CORE/src/Event/EventManager.php:309
Cake\Event\EventManager->_callListener
CORE/src/Event/EventManager.php:286
Cake\Event\EventManager->dispatch
CORE/src/Event/EventDispatcherTrait.php:92
Cake\Controller\Controller->dispatchEvent
CORE/src/Controller/Controller.php:575
Cake\Controller\Controller->startupProcess
CORE/src/Controller/ControllerFactory.php:96
Cake\Controller\ControllerFactory->invoke
CORE/src/Http/BaseApplication.php:313
Cake\Http\BaseApplication->handle
CORE/src/Http/Runner.php:77
Cake\Http\Runner->handle
CORE/src/Http/Middleware/CsrfProtectionMiddleware.php:169
Cake\Http\Middleware\CsrfProtectionMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
APP/Middleware/DebitoEmAnaliseMiddleware.php:19
App\Middleware\DebitoEmAnaliseMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
APP/Middleware/UsuarioMiddleware.php:20
App\Middleware\UsuarioMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
APP/Middleware/TermosFranquiaMiddleware.php:18
App\Middleware\TermosFranquiaMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
APP/Middleware/CorsMiddleware.php:18
App\Middleware\CorsMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
ROOT/vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php:124
Authentication\Middleware\AuthenticationMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
CORE/src/Http/Middleware/BodyParserMiddleware.php:159
Cake\Http\Middleware\BodyParserMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
CORE/src/Routing/Middleware/RoutingMiddleware.php:161
Cake\Routing\Middleware\RoutingMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
CORE/src/Routing/Middleware/AssetMiddleware.php:68
Cake\Routing\Middleware\AssetMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
CORE/src/Error/Middleware/ErrorHandlerMiddleware.php:126
Cake\Error\Middleware\ErrorHandlerMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
ROOT/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php:60
DebugKit\Middleware\DebugKitMiddleware->process
CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle
CORE/src/Http/Runner.php:58
Cake\Http\Runner->run
CORE/src/Http/Server.php:90
Cake\Http\Server->run

@cnizzardini
Copy link
Owner

Yeah that doesn't make a lot of sense, it's saying that the value getting passed into here: https://github.com/cnizzardini/cakephp-swagger-bake/blob/1.next/src/Lib/OpenApi/SchemaTrait.php#L45 is failing the type check... but "number" is indeed a string. I guess double-check you're not doing anything weird on your end and I'll see if I can reproduce this on my end...

@reinaldoborin
Copy link
Author

reinaldoborin commented Jan 5, 2023

At my api controller to reproduce:

    /**
     * Test.
     *
     * @Swag\SwagDto(class="\App\Dto\RequestBodyDto")
     * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
     * @throws \Cake\Http\Exception\MethodNotAllowedException
     */
    public function try()
{
        $this->request->allowMethod(["post"]);

        $post = [
            'value' => $this->request->getData('value')
        ];
        
        $this->set(compact('post'));
        $this->viewBuilder()->setOption('serialize', 'post');
}

Like i said works fine when i change to integer or string, but not if number.

Anyway, thanks for your time.

@reinaldoborin
Copy link
Author

reinaldoborin commented Jan 5, 2023

And my routes.php


$routes->scope('/api', function (RouteBuilder $builder){
    $builder->setExtensions(['json', 'xml']);
    
    $builder->resources('Test', [
        'only' => [
            'try',
        ],
        'map' => [
            'try' => [
                'method' => 'post',
                'action' => 'try',
                'path' => 'try'
            ]
        ], 
        'prefix' => 'Api'
    ]);

    $builder->connect('/swagger', ['plugin' => 'SwaggerBake', 'controller' => 'Swagger', 'action' => 'index']);
});

@cnizzardini
Copy link
Owner

cnizzardini commented Jan 5, 2023

Remove the @var annnotations from your code, they are confusing the annotation parser and I don't want to dig into this since the 1.x branch is not really actively developed. I did add some test coverage to debug this here though: #503 but specifically this: https://github.com/cnizzardini/cakephp-swagger-bake/pull/503/files#diff-3773e7eb1700eea788f8f81c15eecb7fed9a872786915f9da358220844058017

Won't be releasing that, just gonna merge it in so there was nothing to actually fix in the library.

@cnizzardini cnizzardini added question Further information is requested wontfix This will not be worked on labels Jan 5, 2023
@reinaldoborin
Copy link
Author

Thank you man, worked very well, now i can do everything I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants