encode / apistar

The Web API toolkit. 🛠
https://docs.apistar.com
BSD 3-Clause "New" or "Revised" License
5.57k stars 411 forks source link

ValidationError: All object keys must be strings #663

Closed pyiotr closed 5 years ago

pyiotr commented 5 years ago

Hi

I have an endpoint with returns JSON:

{
  "results": [
    {
      "code": "string",
      "email": "string"
    }
  ]
}

I'm getting this error when I'm trying to create apistar client for this endpoint:

Code to reproduce

import apistar

schema = """
openapi: 3.0.0
info:
  title: Accounts
  version: 1.0.0
servers:
- url: https://api.com/example/1.0.0
paths:
  /users:
    get:
      tags:
      - Account
      responses:
        200:
          description: Get user details
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/users'
components:
  schemas:
    users:
      type: object
      properties:
        results:
          type: array
          items:
            type: object
            properties:
              code:
                type: string
              email:
                type: string

"""

client = apistar.Client(schema)
result = client.request('users')

Error

Traceback (most recent call last):
  File "/Users/piotr.py/Projects/example/.venv/lib/python3.7/site-packages/typesystem/tokenize/positional_validation.py", line 13, in validate_with_positions
    return validator.validate(token.value)
  File "/Users/piotr.py/Projects/example/.venv/lib/python3.7/site-packages/typesystem/fields.py", line 545, in validate
    raise ValidationError(messages=error_messages)
typesystem.base.ValidationError: {'paths': {'/users': {'get': {'responses': {200: 'All object keys must be strings.'}}}}}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "example/example_bug.py", line 39, in <module>
    client = apistar.Client(schema)
  File "/Users/piotr.py/Projects/example/.venv/lib/python3.7/site-packages/apistar/client/client.py", line 22, in __init__
    self.document = apistar.validate(schema, format=format, encoding=encoding)
  File "/Users/piotr.py/Projects/example/.venv/lib/python3.7/site-packages/apistar/core.py", line 75, in validate
    value = typesystem.validate_with_positions(token=token, validator=validator)
  File "/Users/piotr.py/Projects/example/.venv/lib/python3.7/site-packages/typesystem/tokenize/positional_validation.py", line 36, in validate_with_positions
    raise ValidationError(messages=messages)
typesystem.base.ValidationError: {'paths': {'/users': {'get': {'responses': {200: 'All object keys must be strings.'}}}}}

Version 0.7.2

tomchristie commented 5 years ago

The openapi spec states that response codes need to be quoted in YAML (otherwise they're interpreted as ints, rather than string literals, which is what the spec expects.)

tomchristie commented 5 years ago

So: "200": oughta work fine. We really will want so far nicer error messaging for this.

pyiotr commented 5 years ago

OAS3 YAML schema has been generated by editor.swagger.io from valid swagger 2.0 JSON.

pyiotr commented 5 years ago

@tomchristie I replaced 200 with "200" but the client still doesn't work because of missing operationId in my schema. Consider adding a note about this. Original swagger file doesn't work with apistar for the same reason.