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

Number type and decimal #59

Closed
AntoineABI opened this issue Feb 14, 2017 · 7 comments
Closed

Number type and decimal #59

AntoineABI opened this issue Feb 14, 2017 · 7 comments

Comments

@AntoineABI
Copy link
Contributor

We can not use parameters validation with number if we expect float/decimal because there are always parsed as int...

const parsedNumber = parseInt(numberValue, 10);

Change to parseFloat maybe as float is a superset of int. Or Maybe implements a Type('float') decorator or something.

Best regards.

@lukeautry
Copy link
Owner

Hmm, good call. It should probably be a float; will get that change in soon.

@isman-usoh
Copy link
Collaborator

declare float/integer in model

interface Model {
  /**
   * @isFloat ErrorMessage
   */
  public floatValue: number;
  /**
   * @isInt ErrorMessage
   */
  public intValue: number;
}

declare float/integer in parameter

class Controller {
  /**
   * @isFloat floatValue
   * @isInt intValue
   */
  public async hello(@Query() floatValue: number, @Query() intValue: number): Promise<void> {...}
}

@dpinart-uxland
Copy link

I can't make it work
`export class MonitorController {

@Get()
/**
 * @isInt limit
 * @isInt offset
 */
findMonitors(@Query() limit: number, @Query()offset: number, @Query() searchString?: string): Promise<QueryResponse<Monitor>>{
    return Promise.resolve(<QueryResponse<Monitor>>{included: [], currentPage: 1, lastPage: true, firstPage: true, })
}

}`

and the swagger gnerated is:
"parameters": [ { "in": "query", "name": "limit", "required": true, "format": "double", "type": "number" }, { "in": "query", "name": "offset", "required": true, "format": "double", "type": "number" }, { "in": "query", "name": "searchString", "required": false, "type": "string" } ]

@dgreene1
Copy link
Collaborator

I’m reopening so someone can see if this bug really did regress or if there’s simply an issue on @dpinart-uxland’s side.

@dpinart-uxland can you please delete all of your output files (like swagger.json) and rerun tsoa then confirm to us that the issue is still occurring?

@dpinart-uxland
Copy link

I upgraded swagger to spec 3 and it seems to be working now placing the jsDoc at method declaration. I guess for spec 2 (default one) the jsDoc must be placed at parameter declaration level

@WoH
Copy link
Collaborator

WoH commented Dec 4, 2019

Since they both share the same AST parser, both use the format I suggested, the initial implementation was done in 2017 and has changed for both Swagger and OpenAPI. (If I'm wrong, please correct me and I'll certainly take another look)

Anyway, glad it worked out 👍

@WoH WoH removed the help wanted label Apr 21, 2020
@WoH WoH closed this as completed Apr 29, 2020
@mrl5
Copy link
Contributor

mrl5 commented Nov 6, 2021

I know that it's closed however I was able to reproduce and then fix - I guess it would be nice to share it here in case someone is affected:

diff --git a/src/history/eventController.ts b/src/history/eventController.ts
index 3c6d5de..8d50c39 100644
--- a/src/history/eventController.ts
+++ b/src/history/eventController.ts
@@ -12,8 +12,6 @@ import { NotFoundError } from './errors';
  */
 type Time = string;
 
-/**
- * @isInt Year
- */
-type Year = number;
-
 @Route('history/event')
 export class HistoryEventController extends Controller {
     @Get()
@@ -34,10 +32,13 @@ export class HistoryEventController extends Controller {
         }
     }
 
+    /**
+     * @isInt year
+     */
     @Get('/{year}')
     async getEventByYear(
         @Res() notFoundResponse: TsoaResponse<404, NotFoundEvent>,
-        @Path() year: Year,
+        @Path() year: number,
         @Query() lang?: Language,
     ): Promise<SingleHistoryEvent> {
         try {

swagger.json after above changes:

diff --git a/spec/swagger.json b/spec/swagger.json
index 0431628..581d309 100644
--- a/spec/swagger.json
+++ b/spec/swagger.json
@@ -529,7 +529,8 @@
                                                "name": "year",
                                                "required": true,
                                                "schema": {
-                                                       "$ref": "#/components/schemas/Year"
+                                                       "format": "int32",
+                                                       "type": "integer"
                                                }
                                        },
                                        {

so the part that was needed in order to take effect was dropping Year declaration and using number instead:

-        @Path() year: Year,
+        @Path() year: number,

update:
I guess the catch is that type Year was declared also in external dependency and used in eventService.ts -> swagger definition was taken from that one ...

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

7 participants