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

Add support for Marshmallow Tuple field #399

Open
mcs07 opened this issue Feb 28, 2019 · 6 comments
Open

Add support for Marshmallow Tuple field #399

mcs07 opened this issue Feb 28, 2019 · 6 comments

Comments

@mcs07
Copy link

mcs07 commented Feb 28, 2019

marshmallow 3.0.0rc4 adds a Tuple field. It would be nice if the apispec marshmallow plugin could support this.

@lafrech
Copy link
Member

lafrech commented Feb 28, 2019

Yes! Absolutely. Would you like to submit the change?

@mcs07
Copy link
Author

mcs07 commented Feb 28, 2019

This might be a bit trickier than I first thought....

I presume the first thing to do is to add to DEFAULT_FIELD_MAPPING :

marshmallow.fields.Tuple: ("array", None),

But from reading the OpenAPI spec it doesn't seem to be possible to specify the types of the contained items individually. So I think the best we could do is to use anyOf then list the types? And also constrain the length?

{
  "type": "array",
  "items": {
    "anyOf": [
      {"type": "float"},
      {"type": "string"}
    ]
  },
  "minItems": 2,
  "maxItems": 2
}

@lafrech
Copy link
Member

lafrech commented Feb 28, 2019

Since it may contain nested fields, it probably deserves a special treatment in there like List and Dict: https://github.com/marshmallow-code/apispec/blob/dev/apispec/ext/marshmallow/openapi.py#L420.

There's been discussions in #172 about making this easier to extend, but for now, we can just add a case.

@Bangertm
Copy link
Collaborator

How about adding an extension to indicate the order to the user. Maybe something like:

{
  "type": "array",
  "items": {
    "oneOf": [
      {"type": "number"},
      {"type": "string"}
    ]
  },
  "minItems": 2,
  "maxItems": 2,
  "x-order": [
      {"type": "number"},
      {"type": "string"}
    ],
}

If there is enough of a use case we could look look to add something to the standard, but using an extension short term can help validate the use case.

A couple other things I noted in playing around:

  • the spec on arrays calls out using oneOf instead of anyOf
  • swagger editor complains about an additional property for either oneOf or anyOf in a 2.0 spec so we may need to only add that that for 3.x specs
  • interestingly if I put a definition with number, string, number in the oneOf field swagger editor displays an appropriate example

greenape added a commit to Flowminder/FlowKit that referenced this issue Sep 18, 2019
greenape added a commit to Flowminder/FlowKit that referenced this issue Sep 18, 2019
@lafrech
Copy link
Member

lafrech commented Sep 1, 2022

@lukaskoeller
Copy link

I couldn't really follow: Is there now a way to get fields.Tuple() correctly translated to a tuple in the openapi spec?
I set openapi_version="3.1.0" in the APISpec from apispec accordingly but couldn't see any improvement in the generated json.

Can anyone enlighten me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants