Swagger node is taking URL phrase as parameter - node.js

I'm using swagger-node for REST APIs and I've found an absurd issue here.
Here's my one of GET requests:
GET: /students/{id}/{name}
(id is number)
And now I wrote another request:
GET: /students/routeInfo/{id}
But the 2nd request me following error:
Expected type is number, found type string. Which clearly means 2nd request is trying to "mask" 1st request. Which I think shouldn't be.
Am I doing something wrong or is there any other way to handle this?

In your path definitions, changeChange your 'parameters' type: number for type: string
Your path will look like this:
/students/routeInfo/{id}:
x-swagger-router-controller: yourStudentController
get:
tags:
- Student
description: description
operationId: yourOperation
parameters:
- name: id
in: path
description: description id of routeInfo
required: true
type: string
See more at https://swagger.io/docs/specification/paths-and-operations/

The exact same situation is described in the OpenAPI specification v3.0 document: section Path Templating Matching, 3rd example. It is called ambiguous matching, and "... it's up to the tooling to decide which path to use." The tool, of course, being swagger-node.
It is weird though, isn't it!

Related

Static routes in pimcore not working for more than one parameter

I´d like to definie a static route in pimcore. This works find as long as the route does not have more than one parameter. As soon as I add a seconde one, I get the following error message:
You have requested a non-existent parameter "book/".
The resulting files looks like this:
pimcore:
staticroutes:
definitions:
f4a7a318-1d29-4f58-8a3c-d204d33a207a:
name: story
pattern: '/\/stories\/(.*)\/(.*)/'
reverse: /stories/%book/%title
controller: 'App\Controller\DefaultController::storiesAction'
variables: 'book,title'
defaults: null
siteId: { }
methods: null
priority: 0
creationDate: 1635722217
modificationDate: 1635722280
Any ideas?
Thanks in advance
I fiddled around with that a bit and found a solution. In previous versions I was able to use reverse patterns like this: /stories/%book/%title where book and title are the variables. However, it seems that the logic has changed here a bit. The following works now: /stories/%%book/%%title

Priority of paths in Open API 3

I'm using an openapi validator (express-openapi-validator) in my Node.js project and can't figure out how to control the order of the paths matched.
If I have 2 paths such as,
/foo/{type}
parameters:
- name: type
schema:
type: string
enum: ['bar', 'bam']
and
/foo/bar
For a request to /foo/bar, the second path is always matched.
How do I control the precedence of this match?
For a request to /foo/bar, the second path is always matched.
This is the correct and expected behavior. OpenAPI Specification states that specific paths must be matched before similar templated paths - see Path Templating Matching. This is not supposed to be configurable, otherwise the behavior would contradict the specification.
To have requests to /foo/bar handled by /foo/{type}, you'll need to remove the /foo/bar path from the API definition.

Many popup errors when saving intent

I get many pop up errors like so :
Failed to upload action package to AOG (preview)
Request contains an invalid argument.
The query pattern '$SchemaOrg_Number:numberx $SchemaOrg_Number:number2x $SchemaOrg_Number:number3' contains an undefined parameter (name: 'number2x' type: 'SchemaOrg_Number')
when I try to save my intent.
However when I navigate to test my google action , it seems to be working fine (it recognises the parameters when I give my application the same expression structure that I define in my training phrases)
It shouldn't be a problem , but I would like to submit my app for alpha testing, and when I do so I get an error of a similar nature:
The query pattern '$SchemaOrg_Number:numberx $SchemaOrg_Number:number2x $SchemaOrg_Number:number3' contains an undefined parameter (name: 'number2x' type: 'SchemaOrg_Number')
What is causing this error and how can I try and fix it?
Cheers!

validate.js returning error "unknown validator pattern"

I'm trying to use validate.js to validate input from the front end before entering it in the database with node but I'm getting an error that I can't figure out. I've gone over the docs and believe I setup the constraints correctly. The exact error is:
message:"Unknown validator pattern"
my validator is setup like this:
let alphanumeric = /^[a-zA-Z0-9]*$/;
let constraints = {
clientUsername:{
presence: true,
length: {min:8, max:15},
pattern:alphanumeric,
message: 'make sure client username is between 8-15 characters, is only numbers and letters'
},
tileCategory:{
presence:true,
length:{min:1, max:1},
numericality:{
onlyInteger:true,
lessThanOrEqualTo:tileCategoryNumber,
},
message:'enter a number, 1 char in length, less than or equal to 3' //the current number of tiles
}
};
validate({clientUsername: input.clientUsername},constraints);
At first I thought it was the regex pattern but tried commenting that out and then it said
message:"Unknown validator messsage"
so I'm guessing there is something wrong with my validator in general.
at the very top I of course included const validate = require('validate.js');
Something similar to this just burned me, have a look at the documentation again.
pattern is sort of a sub-validator of format and should look like:
{
format: {
pattern: "[A-Za-z0-9]+"
}
}
You're trying to use pattern at the "top level". I don't see anything in the documentation that implies helper patterns like alphanumeric exist. (I think the language the tool would use is to say "pattern is an option of the format validator" but I'm not sure.)
Your stated error message also implies a misspelling: it tells you it doesn't recognize messsage, which has 3 of the letter 's' but should have 2.
There's (2) things I could see being the issue. Firstly, you're using JS based regexes with the preceding and following /. Try removing these.
Beyond that, I'd recommend trying to remove the alphanumeric parameter & input the regex directly... it may be a type issue as well.
pattern:"^[a-zA-Z0-9]*$",
Hope this helps! :)

Swagger API "required" - how much is this required?

I've created a Swagger (on nodejs/express) test API with this specification (only relevant part):
...
parameters:
- name: name
in: query
required: true
type: string
...
But I can call the url with empty paramter, for example
http://localhost/test?name=
And it works without any problem, throws no exception or any other sign. Why?
If I make a similar call from the terminal via curl or via postman, it works as well. I parsed the query from the request object and found that in this case, the query parameter is interpreted as an empty string.
Making the call via SwaggerUI is different though, as the UI will actually not make the call UNLESS the query field has a value.
Try doing console.log(req.query); in your handler. You will probably see {name: ''}. Which is legitimate, just that the value of name is an empty string.
Look at JSON4 here: Representing null in JSON. So name IS defined, but it's empty.
You will probably need to do a check for empty string values.
I hope this helps!

Resources