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

Empty strings as valid values for URI parameters #72

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/expand-uri-template-with-parameters.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ expandUriTemplateWithParameters = (uriTemplate, parameters) ->
for uriParameter in uriParameters
param = parameters[uriParameter]

if param.example
if param.example?
toExpand[uriParameter] = param.example
else if param.default
else if param.default?
toExpand[uriParameter] = param.default
else
if param.required
Expand All @@ -54,7 +54,7 @@ expandUriTemplateWithParameters = (uriTemplate, parameters) ->
document: #{uriParameter}\
""")

if param.required and param.default
if param.required and param.default?
result.warnings.push("""\
Required URI parameter '#{uriParameter}' has a default value.
Default value for a required parameter doesn't make sense from \
Expand Down
16 changes: 11 additions & 5 deletions src/validate-parameters.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ validateParameters = (params) ->
result = {warnings: [], errors: []}

for paramName, param of params
if param.required and not param.example and not param.default
if param.required and not (param.example? or param.default?)
text = "Required URI parameter '#{paramName}' has no example or default value."
result.errors.push(text)

switch param.type
when 'number'
if isNaN(parseFloat(param.example))
text = "URI parameter '#{paramName}' is declared as 'number' but it is a string."
if param.example? and isNaN(parseFloat(param.example))
text = "URI parameter '#{paramName}' is declared as 'number' but its example value '#{param.example}' is not."
result.errors.push(text)
if param.default? and isNaN(parseFloat(param.default))
text = "URI parameter '#{paramName}' is declared as 'number' but its default value '#{param.example}' is not."
result.errors.push(text)
when 'boolean'
if param.example isnt 'true' and param.example isnt 'false'
text = "URI parameter '#{paramName}' is declared as 'boolean' but it is not."
if param.example? and param.example isnt 'true' and param.example isnt 'false'
text = "URI parameter '#{paramName}' is declared as 'boolean' but its example value '#{param.example}' is not."
result.errors.push(text)
if param.default? and param.default isnt 'true' and param.default isnt 'false'
text = "URI parameter '#{paramName}' is declared as 'boolean' but its default value '#{param.example}' is not."
result.errors.push(text)

if param.values.length > 0
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/api-blueprint/default-required-boolean.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FORMAT: 1A

# Beehive API

## Honey [/honey{?exampleBool}]

### Get [GET]

+ Parameters
+ exampleBool: `false` (required, boolean)

+ Response 200
4 changes: 4 additions & 0 deletions test/fixtures/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ fixtures =
apiBlueprint: fromFile('./api-blueprint/example-parameter.apib')
swagger: fromFile('./swagger/example-parameter.yml')
)
defaultRequiredBoolean: fixture(
apiBlueprint: fromFile('./api-blueprint/default-required-boolean.apib')
swagger: fromFile('./swagger/default-required-boolean.yml')
)

# Specific to API Blueprint
unrecognizable: fixture(
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/swagger/default-required-boolean.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
swagger: "2.0"
info:
version: "1.0"
title: Beehive API
description: A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
host: petstore.swagger.io
basePath: /
schemes:
- http
consumes:
- application/json
produces:
- application/json
paths:
/honey:
get:
parameters:
- name: exampleBool
in: query
type: boolean
required: true
default: 'false'
responses:
200:
description: Sample description
schema:
$ref: '#/definitions/Pet'
definitions:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
19 changes: 19 additions & 0 deletions test/integration/compile-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,23 @@ describe('compile() · all API description formats', ->
)
)
)

describe('with example boolean value of URI parameter', ->
transaction = undefined
errors = undefined

fixtures.defaultRequiredBoolean.forEachDescribe(({source}) ->
beforeEach((done) ->
compileFixture(source, (args...) ->
[err, compilationResult] = args
transaction = compilationResult.transactions[0]
done(err)
)
)
it('expands the request URI with the example value', ->
assert.equal(transaction.request.uri, '/honey?exampleBool=false')
)
)
)

)
Loading