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

A common use case I run into is very simple: a parameter must be one string out of several predefined values (e.g. the parameter "pet" can be only be "dog" or "cat"). This seems ridiculously simple to add Swagger support for, if confined to that case (let's call it 'string-only-oneOf').

I understand that supporting a more generic case is harder, and that it may be redundant, if you eventually implement that generalized support, to have first supported only the simple (string-only-oneOf) case. But in making the decision not to support any such cases, in order to come up with (or devote resources to) the more generalized solution, what seems to be missing from this discussion is any consideration of whether those other cases are worth the delay in solving the simple (string-only-oneOf) case.

Just how often do these other cases arise relative to the simple case? At some point, if the simple case is very common, and the other cases are very uncommon, isn't it worth it to bite the idealistic bullet and solve the simple case separately from the generalized case?

Or even more simply put: at some point, if there is enough demand for the simple case, then, regardless of how much that demand is relative to the other cases, if there is a simple solution to that simple case, won't it be worth implementing?

At the least it might be useful, if it hasn't already been done, to take some sort of gauge (an informal survey, perhaps) of what the demand might be just of that one simple case.

mewalig avatar Jan 08 '16 20:01 mewalig

@mewalig Doesn't the existing enum support do exactly what you need?

abstratt avatar Jan 08 '16 20:01 abstratt

@abstratt: Man I wish I had seen that earlier. Thanks! (am I supposed to delete my original comment or leave it there?)

mewalig avatar Jan 08 '16 20:01 mewalig

@mewalig - I tend to think it's better to keep things intact. Even posts like that can help us understand what may be wrong, if not in implementation than in documentation.

@abstratt - thanks for providing the answer.

webron avatar Jan 08 '16 20:01 webron

A different opportunity around parameters:-

I am looking for oneOf for defining parameters with a type of in: header (not that is header specific necessarily). We support either a HTTP Basic Authorization Header or a token based Authentication-Token header. I cannot set them both to required: true as it is one or the other not both, nor can I set them both to required: false which would indicate the user need not pass either.

So in this case it is more ambiguous to the client not to have it. That I understand the implementation complexities for the tooling though so it is a rock and a hard place. I appreciate not many people need this type of functionality but when you do the option of putting the information in the description fields or setting both parameters to true and living with getting both headers means neither are my favourite options.

ghost avatar Jan 13 '16 00:01 ghost

@JockMacMad - specifically for such headers, we have the security section, which supports describing an either/or security scheme so your use case can be described now. And as an added bonus, you don't have to repeat the definition for each operation and define it globally if it indeed applies to all operations.

webron avatar Jan 13 '16 01:01 webron

@webron Indeed I have such a section defined for HTTP Basic and OpenToken. It looks look I need to pour over the spec again I think I missed something... and I don't want to dig myself deeper into the hole I just started :)

ghost avatar Jan 13 '16 01:01 ghost

Not at all. This is why we're here - to help make sense of things. Check out the securityDefinitions under the Swagger Object where you can define your schemes. Anything that you define under one scheme would have an AND between them. Then look into security under the Swagger Object or security under the Operation Object to define what's required globally or at the operation level respectively. That value is an array of schemes as defined in the Security Definitions, and between these you'll have an OR.

For your case, you'll create two schemes, and refer to both in the array.

webron avatar Jan 13 '16 01:01 webron

@webron Okay I got it figured out.Thanks for that I had read it about 20 times already but still managed to not quite grasp it :)

I had the security definition but not got the security working on the operation. Shame the token support means I have to set the type: oauth2 as we use OpenToken but I can live with that, there is always a compromise to be had somewhere, and the scopes definition is a bonus for sure. The only downside is none of what I set shows in the HTML in the Editor i.e. no Auth types supported, the necessary headers for the call, nor the scopes (the scopes I can concede is not for the client but for the guy implementing the server side working from the definition it would have been a boon to see it in the HTML).

Again thanks for taking the time to reply.

ghost avatar Jan 13 '16 01:01 ghost

Parent: #579.

webron avatar Mar 27 '16 22:03 webron

Hi. Is this the correct issue for tracking the inclusion of anyOff and oneOff into the first version of the OpenAPI Initiative spec? I have found many links back in here, but as the title says "Support for Either", I think it's worth the clarification.

The journey of trying to find a single issue that dealt with anyOff / oneOff took me to many different github issues and google group conversations, and I often encountered the response that this was "something to be considered for the next version".

I am encountering more and more users of RAML that went there simply because they could model their APIs using any part of JSON Schema. Granted, JSON Schema is not perfect and draft 4 seems a bit unattended, but it's still a spec many RESTful endpoints used before Swagger came along. So telling them to use Swagger is an uphill battle, specially when they are using the parts of the JSON Schema spec that are missing. If they can't model their current endpoints, why would they use Swagger? Nice tooling won't make up for an endpoint definition that doesn't properly describe or restrict the model. This is not meant to sound overcritical, it's just meant to report back what we're seeing in the field with customers.

My suggestion to the Open API Initiative members is to share this progression more openly, so that the whole community can help solve this. What are the specific blockers for code gen that anyOf/oneOff are causing? Which languages? Which swagger OS projects would work, and which would be broken as-is? Can language-specific limitations be placed on certain tool generators?

There's a lot of Swagger users out there that can help. If this information is already openly shared, a link in here would be great.

arthurdm avatar Apr 04 '16 20:04 arthurdm

Hi Arthur, this is a fine place for your comment. It is not currently supported and not clear if it will be. Just because one can model in another format doesn't make a good case for adding it to the OAS on it's own, but we are seriously considering it. There are many problems with doing so, including proposed changes in JSON Schema draft 5, tooling considerations, etc. Supporting this may give end users enough rope to certainly hang themselves, so it's being considered carefully. Please just track this one, we're addressing the inclusion (or not) very soon.

fehguy avatar Apr 04 '16 20:04 fehguy

@arthurdm thanks for the comment. Some of the issues are discussed in #333 (in... length).

As @fehguy mentioned above, just because another tool supports it, doesn't mean we should too. Our goal is not necessarily to attend all use cases (and the existing variety of options help).

We may end up saying we support these and and force the tools to try and handle the issues - I'm not sure we'll be doing our users a better service there though as some cases would require a lot of work to be supported (if they can be at all). That would lead to a different type of frustration - what's the point to be able to describe something if you can't actually use it? This is not just about the Swagger tooling (which are external to the OAI), but also true to any other tool developed out there. If we make it complicated to support, we may end up with less tools in the echo system. Do we tell users there are limitations? Do we tell tool developers they need to work harder? Just need to find the right balance.

webron avatar Apr 04 '16 20:04 webron

anyOf is fundamental to a declarative schema. It's the or to allOf's and.

But I guess I care more about properly describing my API than having mediocre tooling around an improperly described API...

(And, sure, add more special cases like security rather than a general mechanism... THAT will make things easier to support... ??!?)

pdo400 avatar Apr 05 '16 22:04 pdo400

huh?

fehguy avatar Apr 05 '16 23:04 fehguy

+1 I also cannot understand the priorities here. What do I need "tools" for if the spec is unable to describe the API of my endpoints. I am also not sure why some guys here are telling the other how bad their API must be, knowing nothing but the "oneOf" requirement about the case.

Regards, Witold Szczerba 6 kwi 2016 00:59 "Patrick Whitesell" [email protected] napisał(a):

anyOf is fundamental to a declarative schema.

But I guess I care more about properly describing my API than having mediocre tooling around an improperly described API...

(And, sure, add more special cases like security rather than a general mechanism... THAT will make things easier to support... ??!?)

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/OAI/OpenAPI-Specification/issues/57#issuecomment-206025712

witoldsz avatar Apr 06 '16 06:04 witoldsz

A common case is expanding entities. For example, using the Stripe Api, it may return a string, int, or null by default, but if you ask for the entire entity, it will return an object instead.

Granted, I believe I could specify each type it could be and copy every property of the entity, but in doing so, I lose a lot of reusability, which is one of the reasons I chose to use the spec.

In my opinion, it's odd to let the spec be defined by what's possible in a subset of languages or tools. If the problem is with statically typed languages, that sounds like a problem for implementations in those languages to solve. If the problem is with code generation tools, that sounds like a problem for those projects to solve.

natebrunette avatar Apr 27 '16 14:04 natebrunette

Limiting the API Spec just because the tooling around it cannot support it does not make sense.

Here is a use case from the retail/search domain:- A keyword search API could return oneOf

  1. Product Search Results
  2. A keyword search redirect (Searching for special list of keywords could be redirected to special pages)

Keyword search API:

Endpoint: GET /search

Parameters: type=keyword q=makeup

Response could be any one of:-

  1. Search Results
{
  "totalProducts": 77,
  "products": [
    ......
  ],
  "refinements": [
    .......
  ],
  "categories": [
    .......
  ],
  "keyword": "RAV4"
}
  1. Search Redirects
{
  "searchRedirectTarget": {
    "targetScreen": "product",
    "targetValue": "P123456;skuId=246810",
    "targetUrl": "/my-toyota-rav4"
  },
  "keyword": "RAV4"
}

lijo-jacob avatar May 01 '16 18:05 lijo-jacob

If OpenAPI wants to be agnostic about design-first vs code-first, then the specification has to be able to adequately describe real world APIs.

I don't think any decisions about inclusion in the specification should be determined by how easy or hard it is for a tool to implement. That is a problem that can be solved elsewhere, and if tool authors don't want to deal with it, then let them implement workarounds. By omitting something like this from the spec entirely you just force the problem on the users.

MatMoore avatar May 01 '16 20:05 MatMoore

Umm, allowing users to describe anything but have no tooling support doesn't change the problem being forced on the user - it just moves the problem from the design phase to the implementation or use phases. This is about keeping an ecosystem that works and plays well together - and yes, that may impose a limitation on design. Reiterating what's been said all along - supporting the entire spectrum of API designs is not the goal, but we're definitely looking to expand the percentage with each release, in a sustainable manner. Yes, some real world APIs will not be described by the OAS.

This is an old thread, and I don't remember all the details about it, and my opinions change over time too. Sometimes the problem is not with the 'what' we want to support but rather 'how'. This is definitely an issue we're going to try and tackle. We have yet to start dealing with this request.

webron avatar May 02 '16 19:05 webron