swagger-api / swagger-api/swagger-codegen
Am I using discriminator right?
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
The OpenAPI v2.0 spec defines discriminator and shows how to define objects with it but not how it can be actually used.
I have code like this
Swagger declaration file content or url
swagger: '2.0'
info:
title: "sscce"
version: '1.0'
description: sscce
definitions:
Animal:
type: object
discriminator: animal_type
properties:
animal_type:
type: string
Bear:
allOf:
- $ref: "#/definitions/Pet"
- type: object
required:
- color
properties:
color:
type: string
enum:
- white
- brown
Cat:
allOf:
- $ref: "#/definitions/Pet"
- type: object
required:
- likes_water
properties:
likes_water:
type: boolean
paths:
/foo:
post:
parameters:
- name: animal
in: body
schema: {$ref: "#/definitions/Animal"}
responses:
"200":
description: foo
type: string
and what I am trying to do is essentialy define an algebraic sum type Animal = Cat | Bear meaning a parameter defined as of type Animal must contain either a type of Cat or a type of Bear.
But when I do this, the parameter animal is defined just as plain Animal and the types Cat and Bear are not even shown to be present in the spec.
First thing I could do is to forget about the subtypes and just put the attributes into Animal but then it doesn't make much sense as Bears universally do not have a problem with water (if I am wrong here, please bear (see what I did there) with me; I'm doing my best to come up with sane analogy) and then cats can't really be universally assigned a color attribute as some are rather patterned. Anyhow, I'm screwed.
Another thing I could do is to defined separate POST paths like
/animal/bear:
post:
-name: bear
schema: {$ref: "#/definitions/Bear"}
and similar for cat ... and while that would work and generally I'd be ok with it but I need these as part of another algebraic product type (complex type, user defined type, whatever ...) where I could use a similar approach but in the real case my Animal types have a natural primary key so I can lump them together within the containing type into a single POST request, cutting down latencies on saved roundtrip.
I guess this is basicly
https://github.com/OAI/OpenAPI-Specification/issues/57
https://github.com/chop-dbhi/serrano/pull/302
and I am aware of the oneOf feature in OpenAPI v3.0 but
- swagger-codegen doesn't seem to support v 3.0 and I'm not even sure if v3.0 is finalized
- is there something I can do with this in v2.0 and
- am I misunderstading discriminator?
Swagger-codegen version
current master; HEAD = 1c160df33897898da1c39f230c737eb6a313e6ba
Command line used for generation
JAR=".../swagger-codegen.git/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
java -jar "${JAR}" generate -i ~/sscce.yaml -l html2 -o foo
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied sscce.yaml and the swagger-codegen CLI command, then inspect how the OpenAPI v2 discriminator is represented in the generated html2 output. Review referenced issue #57 and the OpenAPI 3 oneOf discussion; done means establishing whether v2 supports this polymorphic model and documenting or scoping the required behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100