Code Monkey home page Code Monkey logo

Comments (6)

alex-zywicki avatar alex-zywicki commented on June 15, 2024 1

@dedoussis close away

from asynction.

alex-zywicki avatar alex-zywicki commented on June 15, 2024

@dedoussis If I am interpreting things correctly I think that the special case for the array is incorrect and can be removed. Or that some checking needs to be done against the args parameter.

from asynction.

dedoussis avatar dedoussis commented on June 15, 2024

This condition branching is there to account for the case of the handler accepting multiple args. In the case of multiple args, the payload schema is a tuple array. See the payload structure table in the middle of this section of my article: https://www.asyncapi.com/blog/socketio-part2#channels

In the case of a tuple payload, all args need to be validated and not just the first one.
However, I think I have not accounted for the case of the payload being an array rather than a tuple. In the case of an array, we will only need to validate the first arg. Hence we need to have an extra nested condition within if schema_type == "array" to check if there is a prefixItems field or not:

if schema_type == "array":
    if schema.get("prefixItems"):  # tuple
        jsonschema_validate_payload(args, schema)
    else:  # array
        jsonschema_validate_payload(args[0], schema)

from asynction.

alex-zywicki avatar alex-zywicki commented on June 15, 2024

@dedoussis I guess what I don't understand is why in the one case where the payload is an array but does not have prefixItems that args would be a tuple containing an array, but in the prefixItems` case that args itself would be the array?

Why would:

type: "array"

Result in ([1,2,3], ) as args

But

type: "array"
prefixItems:
    ...

Result in (1,2,3)

My interpretation of the second case there is that there were multiple payloads rather than an array payload. from everything I've seen (using python-socketio as the client and asynction) the args on a message handler will always only have one item (the payload)

from asynction.

dedoussis avatar dedoussis commented on June 15, 2024
type: "array"
items:
  type: number

The above payload signals that there should only be a single argument of type Sequence[str], ie args == ([1,2,3], ).
Using python-socketio as the client, an event emission of such a payload would look like this:

c.emit("foo-event", data=[1,2,3])

Under the hood, the python-socketio client does some magic to convert non-tuple data to a tuple of a single element, so that [1,2,3] becomes ([1,2,3], ).

And in asynction the handler would look like this:

def on_foo_event(nums: Sequence[int]) -> None:
    ...

Whereas:

type: "array"
prefixItems:
- type: string
- type: number

signals that there should be 2 arguments (one of type string and one of type int, ie args == ("bar", 1))

Using python-socketio as the client, an event emission of such a payload would look like this:

c.emit("foo-event", data=("bar", 1))

Here we pass a tuple as data, so python-socketio won't do any magic conversion.

And in asynction the handler would look like this:

def on_foo_event(word: str, num: int) -> None:
    ...

from asynction.

dedoussis avatar dedoussis commented on June 15, 2024

This fix has been released as part of version 0.7.0. @alex-zywicki If you're happy with it, I can close the issue.

from asynction.

Related Issues (16)

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.