diegohaz / diegohaz/rest

Annotations don't contain dataType in case fo yo rest:api

Open
#265 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.8k
Forks
304
PR merge metrics
No merged PRs in 30d

Description

Possible issue for: `error: Cannot read property 'indexOf' of undefined`

On receiving the above error, I went thru the code and found out that:

When created an API by using `yo rest:api`, it doesn't generate the datatype in annotations for `@apiParam`. See a sample annotation generated below for `products`

```
/**
* @api {post} /products Create product
* @apiName CreateProduct
* @apiGroup Product
* @apiParam name Product's name.
* @apiParam description Product's description.
* @apiParam price Product's price.
* @apiSuccess {Object} product Product's data.
* @apiError {Object} 400 Some parameters may contain invalid values.
* @apiError 404 Product not found.
*/
router.post('/',
body({ name, description, price }),
create)
```

Whereas, the user model generated by `yo rest` does contain the datatype `{String}` for @apiParam `access_token`.

```
/**
* @api {get} /users Retrieve users
* @apiName RetrieveUsers
* @apiGroup User
* @apiPermission admin
* @apiParam {String} access_token User access_token.
* @apiUse listParams
* @apiSuccess {Object[]} users List of users.
* @apiError {Object} 400 Some parameters may contain invalid values.
* @apiError 401 Admin access only.
*/
```

I checked the code and there's a small fix required in this line, https://github.com/diegohaz/rest/blob/master/generators/api/templates/index.js#L71

I see the template in this line that could be used
https://github.com/diegohaz/rest/blob/master/generators/app/templates/api/user/index.js#L98

I understand that the user model already has the datatypes defined in annotations because the logic is defined for each param, and the `yo rest:api` command doesn't accept datatypes from the CLI.
I am suggesting that since the generator already assigns the datatype `String` to the params we create for the API, so let's assign "String" as the param data type in annotations.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with generators/api/templates/index.js at the referenced line and compare it with generators/app/templates/api/user/index.js around line 98. Run yo rest:api to inspect the generated annotations, then confirm that the generated @apiParam entries include the String datatype and that the reported error no longer occurs.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
tooling
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.