boj
boj

Reputation: 11395

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?

Upvotes: 0

Views: 304

Answers (1)

Michael Jonas
Michael Jonas

Reputation: 36

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!

Upvotes: 1

Related Questions