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

Issue with Boolean types in code generated by swagger-codegen #127

Closed
scolby33 opened this issue Feb 1, 2016 · 9 comments
Closed

Issue with Boolean types in code generated by swagger-codegen #127

scolby33 opened this issue Feb 1, 2016 · 9 comments
Assignees

Comments

@scolby33
Copy link
Contributor

scolby33 commented Feb 1, 2016

Here is a minimal (non-)working example needed to reproduce this issue.

test.yml:


---
swagger: '2.0'

info:
    version: '0'
    title: 'Boolean Test'

paths:
    /test:
        get:
            description: A test endpoint.
            parameters:
                - name: truthiness
                  in: query
                  description: A test parameter.
                  type: boolean
                  default: true
            responses:
                204:
                    description: Do some magic.

Process this file via swagger-codegen:

java -jar /usr/local/Cellar/swagger-codegen/2.1.5/libexec/swagger-codegen-cli.jar generate -i ./test.yml -l python-flask -o .

Here is the resulting swagger.yml:


---
swagger: "2.0"
info:
  version: "0"
  title: "Boolean Test"
paths:
  /test:
    get:
      tags:
      - "default_controller"
      description: "A test endpoint."
      operationId: "controllers.default_controller.test_get"
      parameters:
      - name: "truthiness"
        in: "query"
        description: "A test parameter."
        required: false
        type: "boolean"
        default: true
      responses:
        204:
          description: "Do some magic."
definitions: {}

Running this with python app.py results in the following error:

Traceback (most recent call last):
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/decorators/validation.py", line 75, in validate_type
    return make_type(value, param_type)
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/decorators/validation.py", line 53, in make_type
    return type_func(value)
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/utils.py", line 135, in boolean
    raise ValueError('Invalid boolean value')
ValueError: Invalid boolean value

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/operation.py", line 118, in validate_defaults
    validation.validate_type(param, param['default'], 'query', param['name'])
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/decorators/validation.py", line 77, in validate_type
    raise TypeValidationError(param_type, parameter_type, parameter_name)
connexion.decorators.validation.TypeValidationError: Wrong type, expected 'boolean' for query parameter 'truthiness'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "app.py", line 8, in <module>
    app.add_api('swagger.yaml', arguments={'title': ''})
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/app.py", line 123, in add_api
    validate_responses=validate_responses)
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/api.py", line 95, in __init__
    self.add_paths()
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/api.py", line 136, in add_paths
    self.add_operation(method, path, endpoint)
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/api.py", line 117, in add_operation
    validate_responses=self.validate_responses, resolver=self.resolver)
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/operation.py", line 103, in __init__
    self.validate_defaults()
  File "~/.virtualenvs/swagger/lib/python3.5/site-packages/connexion/operation.py", line 122, in validate_defaults
    param_type=param['type']))
connexion.exceptions.InvalidSpecification: <InvalidSpecification: The parameter 'truthiness' has a default value which is not of type 'boolean'>

Manually changing boolean in swagger.yml to bool or Boolean causes the script to run without error, but then the datatype in the Swagger UI is incorrect:
screen shot 2016-01-31 at 19 43 39

Finally, changing boolean to bool or Boolean in the original test.yml causes the following error with swagger-codegen:

reading from ./test.yml
[main] INFO io.swagger.codegen.DefaultCodegen - generated operationId testGet   for Path: get /test
[main] WARN io.swagger.codegen.DefaultCodegen - Type is NULL for Serializable Parameter: io.swagger.models.parameters.QueryParameter@795a5d7b
[main] WARN io.swagger.codegen.DefaultCodegen - warning!  Property type "null" not found for parameter "truthiness", using String
writing file ./swagger/swagger.yaml
writing file ./app.py
writing file ./README.md

I was also going to open an issue with swagger-codegen, but given the fact that altering the name of the type messes up its representation in the Swagger UI indicates to me that the problem is likely on connexion's end.

@hjacobs
Copy link
Contributor

hjacobs commented Feb 1, 2016

@scolby33 thanks for the bug report, we'll look into it (apparently @rafaelcaricio will tackle it)..

@rafaelcaricio
Copy link
Collaborator

I was investigating on this issue and I found out that the default property should be "true" (with quotes) in your fist example and then it would work. But I tried to run locally to reproduce the fix, but I did not worked. So I dig into more on that issue...

I found out that the Swagger model converts the default property to a native boolean value. And since the Connexion Codegen simply serializes the swagger definition to yml to be used when running the app by Connexion it breaks.

If you change the generated yml default value to "true" (with quotes) it works.

I guess that is an issue in all generators that output swagger definitions like that.

@rafaelcaricio
Copy link
Collaborator

Giving a second thought on it. Maybe an option is to make Connexion accept literal boolean values and not only string boolean values as default value.

@rafaelcaricio
Copy link
Collaborator

Yes, we should update Connexion (ref. swagger-api/swagger-core#1368)

@scolby33
Copy link
Contributor Author

scolby33 commented Feb 1, 2016

I can confirm that placing quotes around the default values fixes the issue.

Is the issue that YAML expects that Boolean values are quoted? Here in the spec, it seems that Booleans can be true or false, without quotes. This is matched in the Ansible documentation (scroll down a bit), which was the first place I could find an example with a Boolean value in YAML. Both of these sources seem to indicate that no quotes should be required.

I'm also confused as to why default: "true" is necessary, but required: true works just fine. Whatever YAML parser is being used chokes on Booleans only sometimes?

@rafaelcaricio
Copy link
Collaborator

Yes, I searched further and we should accept in Connexion the true literal. I am working on it right now.

@scolby33
Copy link
Contributor Author

scolby33 commented Feb 1, 2016

Oh, I see we commented at the same time. Thanks for the update!

@hjacobs
Copy link
Contributor

hjacobs commented Feb 2, 2016

@scolby33 the fix was released in https://github.com/zalando/connexion/releases/tag/1.0.49

Can you verify that it works for you? Thanks!

@scolby33
Copy link
Contributor Author

scolby33 commented Feb 2, 2016

Everything looks good! Thank you for your quick response and fix for this issue.

@scolby33 scolby33 closed this as completed Feb 2, 2016
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

3 participants