Code Monkey home page Code Monkey logo

Comments (6)

hh-h avatar hh-h commented on June 14, 2024

Please, provide a reproducible example: handler spec + input data.

from aiohttp-swagger3.

eLvErDe avatar eLvErDe commented on June 14, 2024

Ok, tomorrow

from aiohttp-swagger3.

eLvErDe avatar eLvErDe commented on June 14, 2024

Hi,

Edited message. Actually it does behave incorrectly imho.

from aiohttp import web
from aiohttp_swagger3 import SwaggerDocs, SwaggerUiSettings

routes = web.RouteTableDef()


@routes.post("/bug60/addProp")
async def addProp(request: web.Request, body) -> web.Response:
    """
    ---
    summary: Mixed type array as additionalProperties value
    tags:
      - bug60
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            additionalProperties:
              type: array
              items:
                oneOf:
                  - type: string
                  - type: number
            example:
              key: ["abc", 1, 1.1]
    responses:
      '200':
        description: Return original payload
    """

    for key, val in body.items():
        for idx, value in enumerate(val):
            print('Value %d for "key" %s is %s (%s)' % (idx, key, value, value.__class__.__name__))
    return web.json_response(body)


@routes.post("/bug60/array")
async def array(request: web.Request, body) -> web.Response:
    """
    ---
    summary: Mixed type array
    tags:
      - bug60
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: array
            items:
              oneOf:
                - type: string
                - type: number
            example: ["abc", 1, 1.1]
    responses:
      '200':
        description: Return original payload
    """

    for idx, value in enumerate(body):
        print("Value %d is %s (%s)" % (idx, value, value.__class__.__name__))
    return web.json_response(body)


def main():
    app = web.Application()
    s = SwaggerDocs(app, swagger_ui_settings=SwaggerUiSettings(path="/"))
    s.add_routes(routes)
    web.run_app(app)


if __name__ == "__main__":
    main()

Console will print:

Value 0 is abc (str)
Value 1 is 1.0 (float)
Value 2 is 1.1 (float)

Second value is expected to be an integer

from aiohttp-swagger3.

eLvErDe avatar eLvErDe commented on June 14, 2024

If you add - type: integer to the allowed type, it ends up with:

400: {"body": {"1": "fail to validate oneOf"}}

from aiohttp-swagger3.

hh-h avatar hh-h commented on June 14, 2024

OpenAPI has two numeric types, number and integer, where number includes both integer and floating-point numbers.

https://swagger.io/docs/specification/data-models/data-types/#numbers

numbers include integers, so both checks are succeeded and this is not allowed by oneOf.

oneOf – validates the value against exactly one of the subschemas

Your case is weird, indeed, but the library logic is correct.

Why don't you use anyOf instead?

Value 0 is abc (str)
Value 1 is 1 (int)
Value 2 is 1.1 (float)

from aiohttp-swagger3.

eLvErDe avatar eLvErDe commented on June 14, 2024

The API is acting as REST bridge to another protocol, so I don't care about what the payload actually contents, it's up to the next protocol to deal with it (but it needs to receive properly typed items).
Thanks for your suggestion, I'll give anyOf a try

from aiohttp-swagger3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.