Skip to content

discriminator support? #46

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

Closed
fingon opened this issue Jan 27, 2016 · 3 comments
Closed

discriminator support? #46

fingon opened this issue Jan 27, 2016 · 3 comments

Comments

@fingon
Copy link

fingon commented Jan 27, 2016

Swagger 2.0 has 'discriminator' field, which is essentially used to choose the schema to use based on contents of the field. At least apispec 0.6 does not have this (nor does actually marshmallow itself, +- marshmallow-polyfield which is similar but helps choose type of field instead of the whole object's schema).

Any plans on this?

@sloria
Copy link
Member

sloria commented Mar 6, 2016

There are no near-term plans for this, but I would certainly review a PR for it.

@lafrech
Copy link
Member

lafrech commented Feb 14, 2017

Ideally, I would add

discriminator = 'field_name'

to the Meta of the Schema.

But if I do that, I don't know where to get 'discriminator' back. It is lost in SchemaOpts.__init__ in Marshmallow, which only picks a whitelist of attributes.

Also, it is a bit misleading that discriminator appears in _VALID_PROPERTIES, which is used in field2property but not in schema2parameters. AFAIK, it is only useful for a schema, not for a field.

Right now, what I do is add 'x-discriminator': 'field_name' when calling definition on my schema:

api.definition('MySchema')(schema, **{'x-abstract': True, 'x-discriminator': 'kind'}

This works because I added a schema definition registration helper to my framework to pass 'x-' attributes to the spec: https://github.com/Nobatek/flask-rest-api/blob/master/flask_rest_api/spec/plugin.py#L111

The whole inheritance mechanism is not well defined in OpenAPI yet so I only rely on x- properties.

@sloria
Copy link
Member

sloria commented Mar 3, 2017

This is now supported via the extra_fields parameter to definition:

spec.definition('Pet', extra_fields={'discriminator': 'pet_type'})

@sloria sloria closed this as completed Mar 3, 2017
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