OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Support for Either

Open ikitommi opened this issue 12 years ago • 50 comments

hi.

Would like to be able present a type of Either[Dog,Cat] (or in clojure (either Dog, Cat, Moose) in the Spec. Quite common construct in the FP land. Would be like a enum, but with values of any type.

Currently such a thing could be presented as a complex type with optional fields for all the value => at least the swagger-ui would be able to generate sample models (with all possible alternatives in place) for it. Not perfect, but ~works. Requires also special coercion in the server side to map back into Either.

http://www.scala-lang.org/api/2.11.0/index.html#scala.util.Either

https://github.com/Prismatic/schema/blob/master/test/cljx/schema/core_test.cljx#L194-L203

ikitommi avatar May 20 '14 08:05 ikitommi

... in JSON Schema this would be implemented most likely with oneOf like:

{
  "oneOf": [
    { "$ref": "Cat" },
    { "$ref": "Dog" }
  ]
}

ikitommi avatar May 20 '14 21:05 ikitommi

Okay, in Swagger 2.0 we explicitly decided not to support this. There are various issues with supporting it ranging from how deterministic such an API is to can most common development languages support this feature easily.

I'm closing this issue for now, but marking it as a proposal for the next version of the spec, so that we may revisit this decision.

webron avatar Sep 19 '14 19:09 webron

I wonder, if I have a resource which sends a response as an array that contains both say articles and ads, how would i define such a response in swagger? Combining all properties in one response object is really ugly and kinda makes validation based on the spec useless. I think oneOf would be a good solution for this case, unless I've missed another way of doing it?

ahultgren avatar Nov 10 '14 10:11 ahultgren

We thrive to describe deterministic APIs. Sending a single array of both articles and ads makes it that much more difficult to process, especially for strongly-typed languages, so we don't support it. If we can find an elegant way to do it, we'll be more than happy to add support for it.

webron avatar Nov 10 '14 10:11 webron

@webron Which languages would be most important to support? I mentioned on IRC the use case of GeoJSON, which I think is an important structure to be able to validate:

{
  "type": "Point",
  "coordinates": [-3.0107731, 16.7733758]
}
{
  "type": "LineString",
  "coordinates": [[-3.0107731, 16.7733758], [-101.8314831,35.2090451]]
}

jphastings avatar Feb 16 '15 12:02 jphastings

I am new to Swagger and have already encountered several areas where this would be useful. With respect to the deterministic-API goal, what are popular-enough strongly-typed languages that would have great difficulty processing, e.g. a heterogenous collection? (As long as the possible types of the collection were explicitly enumerated and were defined (in swagger and the other language) in an appropriate way to indicate a relationship (parent-class/child-class, etc) that played nice with the type system.

davidsklar avatar Mar 27 '15 16:03 davidsklar

I think I understand the reasons for discouraging people from designing APIs where different types of objects can appear in the same place. (The term "non-deterministic API" doesn't seem quite right to me though.)

But anyway, it's a bit of a problem for documenting existing APIs (or GeoJSON, above). I'm working on one at the moment where a certain response contains an array of "stops" and "lines", where each is basically:

{
  "type": "stop", # or "line"
  "result": {
     # other properties depend on type
  }
}

Obviously it's possible to implement this in Swagger but it's pretty hacky and requires duplication and having to explain in words what's going on.

stevage avatar May 25 '15 00:05 stevage

This is not about API design only, this is also related to the ability to provide support for it in the various tools we offer around Swagger. Since there is a workaround for some cases, it may be good enough for now. That said. it does not mean it will not be considered for the next version, we just need to do it wisely.

webron avatar May 26 '15 15:05 webron

What is the workaround for unions, discriminated or otherwise?

The Kubernetes API has a number of such cases. We currently use an object with all optional fields, where only one of the fields is expected. For example, PersistentVolumeSpec may contain details specific to a particular storage backend (swagger 1.2, but I believe it would be similar in 2.0): https://github.com/kubernetes/kubernetes/blob/master/api/swagger-spec/v1.json#L11994

bgrant0607 avatar Aug 18 '15 01:08 bgrant0607

I believe with the spec as-is, you would document the common shared parameters in a base schema and use a discriminator field to represent acceptable concrete types. For the next version of the spec, agreed this needs attention.

fehguy avatar Aug 18 '15 02:08 fehguy

I also agree this is needed to fully support polymorphism.

Fresa avatar Aug 20 '15 10:08 Fresa

Since there is a workaround for some cases, it may be good enough for now.

I've just run into this also, evaluating Swagger 2.0 for our existing API, but I'm not sure what the suggested workaround is?

erikvanzijst avatar Oct 06 '15 06:10 erikvanzijst

@erikvanzijst - the workaround is related to when you have a common property between the options (such as type) which can be used as the discriminator. You can find more information about the discriminator in the spec itself.

https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schemaDiscriminator https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#composition-and-inheritance-polymorphism

webron avatar Oct 06 '15 16:10 webron

Right. I'll have a look at that.

erikvanzijst avatar Oct 06 '15 20:10 erikvanzijst

@webron

We thrive to describe deterministic APIs. Sending a single array of both articles and ads makes it that much more difficult to process, especially for strongly-typed languages, so we don't support it.

This is the most ridiculous explanation I could possibly imagine about not supporting oneOf :flushed:

witoldsz avatar Oct 08 '15 09:10 witoldsz

@witoldsz @webron I think this discussion shifted towards incompatibility with JSON Schema topic. So it became duplicate of #333. Can we move discussion there and close this issue?

IvanGoncharov avatar Oct 08 '15 12:10 IvanGoncharov

@witoldsz - sometimes the wording I use is not the best, but even if you disagree there are probably better ways to express it. If you'd like to discuss it further, I'm all for it.

@IvanGoncharov - while they are certainly related, I think it should remain open. When we start actual work on the next version we'll do a consolidation of issues (there are a few that apply).

webron avatar Oct 09 '15 01:10 webron

The value of the chosen property has to be the friendly name given to the model under the definitions property

@webron, if I understand this properly, all the discriminator values must be declared inside definitions at the root. Does anyone have a workaround for conflicting discriminator values illustrated in the following?

---
definitions:
  resource1:
    discriminator: type
    required:
      - type
    properties:
      type:
        type: string
        enum:
          - custom

  resource2:
    discriminator: type
    required:
      - type
    properties:
      type:
        type: string
        enum:
          - custom

  # resource1.type=custom
  custom: {}

  # resource2.type=custom
  custom: {}

gmathieu avatar Oct 23 '15 14:10 gmathieu

@gmathieu - not sure what you're trying to do there, but this may be a bit more of a support issue. Mind providing more details in the google group?

webron avatar Oct 23 '15 18:10 webron

Thanks for the quick response!

Anyone who's interested in the follow question: https://groups.google.com/forum/#!topic/swagger-swaggersocket/snqVqH_cCRY

gmathieu avatar Oct 23 '15 20:10 gmathieu