OAI / Overlay-Specification

The OAI Overlay Specification
Apache License 2.0
56 stars 15 forks source link

Group multiple parameter definitions for better maintainability #34

Closed cansik closed 3 months ago

cansik commented 9 years ago

Idea

Sometimes you have some parameters you want to provide on every path. At the moment it is possible to reference them with the $ref tag. This works great if you have a lot of different parameter combination for each route.

But we now have a complex api with a lot of optional get parameters for every path. Now the problem we encounter is, that if we have to add new parameters to this get parameters, it is very cumbersome and inconvenient to set the reference to them on each path. Often it happens that you forget one.

It would be great if we could group parameter definitions for a better maintainability and usability.

Example

As already presented on stackoverflow here an example of the definition (how it could look like):

parameters:
  MetaDataParameters:
    # Meta Data Properties
    - name: id
      in: query
      description: Entry identification number
      required: false
      type: integer

    - name: time_start
      in: query
      description: Start time of flare
      required: false
      type: string

    - name: nar
      in: query
      description: Active region number
      required: false
      type: string

And here is how I would like to reference it:

/test/:
  get:
    tags:
      - TEST
    operationId: routes.test
    parameters:
      - $ref: "#/parameters/MetaDataParameters"
    responses:
        200:
          description: OK

To get more on the user experience, this definition is much more readable than 20 parameters for each route.

Other Ideas?

Have you already thought about something like this or do you want us swagger users to go another way to deal with this problem?

Regards Florian

webron commented 9 years ago

Thanks for opening the issue, there's just one thing that's not clear. On one hand, you're saying that you want to provide them for 'every path' which implies a global definition that won't require you to reference it, and on other hand, you go ahead and reference from a specific method to the group of parameters. So which of the two are you looking for? or is it both?

cansik commented 9 years ago

Ok the formulation every path is maybe wrong (too open). We still have paths which do not need this parameter. We would like to use it as described in the example to reference a group of parameter with one reference. Otherwise it would look like this:

/test/:
  get:
    tags:
      - TEST
    operationId: routes.test
    parameters:
      # In path parameter
      - $ref: "#/parameters/event_type"
      - $ref: "#/parameters/appearance_date"

      # Meta Data Properties
      - $ref: "#/parameters/param1"
      - $ref: "#/parameters/param2"
      - $ref: "#/parameters/param3"
      - $ref: "#/parameters/param4"
      - $ref: "#/parameters/param5"
      - $ref: "#/parameters/param6"

      # Data Properties
      - $ref: "#/parameters/param7"
      - $ref: "#/parameters/param8"
      - $ref: "#/parameters/param9"
      - $ref: "#/parameters/param10"

      # Specific Properties
      - $ref: "#/parameters/param11"
      - $ref: "#/parameters/param12"
      - $ref: "#/parameters/param13"
      - $ref: "#/parameters/param14"
      - $ref: "#/parameters/param15"
      - $ref: "#/parameters/param16"
      - $ref: "#/parameters/param17"
    responses:
        200:
          description: OK

With the enhancement we could write it like this and manage the items of each group in one place:

/test/:
  get:
    tags:
      - TEST
    operationId: routes.test
    parameters:
      # In path parameter
      - $ref: "#/parameters/event_type"
      - $ref: "#/parameters/appearance_date"

      # Meta Data Properties
      - $ref: "#/parameters/MetaDataParamGroup"

      # Data Properties
      - $ref: "#/parameters/DataParamGroup"

      # Specific Properties
      - $ref: "#/parameters/SpecificParamGroup"
    responses:
        200:
          description: OK
webron commented 9 years ago

Okay, thank you for the clarification. The reason I asked is because we have a feature request for global parameters already.

People have asked for parameter groups in various places, but nobody opened a feature request, so thank you for taking the time to do so. The value of it is understandable. I'm not sure the syntax you shared works, but we can evaluate it further over time.

mohsen1 commented 9 years ago

If $ref: "#/parameters/MetaDataParamGroup" resolves to a hash (as described above) parameters will have that hash in it which is not valid as Swagger 2.0. Unless we change the parameters array format to allow hashs (which I don't think it's a good idea) it will not work.

cansik commented 9 years ago

@mohsen1 Ok and what is the problem with hashes for parameters? Is there a discussion in another issue about this?

mohsen1 commented 9 years ago

parameters is an array of parameter objects. I'm not sure if we want parameterName:parameterObject hashs in the spec also.

dolmen commented 9 years ago

@cansik, @mohsen1: /parameters is a hash of parameter objects but /paths/{path}/parameters and /paths/{path}/{operation}/parameters are arrays of parameters objects. There is no confusion.

dolmen commented 9 years ago

@cansik For your request, I suggest that you define your own vendor extension for parameter groups, and write a processing tool that would inline your group references into a pure Swagger 2.0 spec for external consumption. This is how I work for my own API: I write my spec in YAML (that allows me to use comments) and will only publish the JSON version of it.

I already find that many tools do not fully support the current spec (for example I found issues with /parameters and /paths/{path}/parameters and I had to write a processor to inline them). We don't need now to complexify the spec if tools do not follow it.

project0 commented 8 years ago

+1 Want to see this feature as well.

0x62ash commented 8 years ago

+1 we too have predefined query params to almost all api paths

renanmt commented 8 years ago

+1 this would save me a lot of time and lines in my doc spec

Growiel commented 8 years ago

+1 this too, everything that gets me to reuse code is a huge +1.

mkostin commented 8 years ago

+1 would love to see it

rafael84 commented 8 years ago

+1

jharmn commented 8 years ago

It's worth noting that RAML take on this, 'traits' (really not much different from the proposed structure). http://raml.org/developers/raml-200-tutorial#traits

ranacseruet commented 8 years ago

+1

fehguy commented 8 years ago

Reference OAI/OpenAPI-Specification#560

clemensberndt commented 8 years ago

+1

lggmonclar commented 8 years ago

+1

ePaul commented 8 years ago

Just as a note for everyone wanting to vote this up: Github recently added a feature allowing a "reaction" to each post, using the button in the top left corner of each post. This will not clutter the thread with contentless comments.

DavidBiesack commented 8 years ago

How about allowing us to organize parameters into sets? Then a path could reference parameter sets to automatically get all the parameters in those sets:

parameters:
    - name: id
      in: query
      description: Entry identification number
      required: false
      type: integer
      sets: [ "MetaData" ]

    - name: time_start
      in: query
      description: Start time of flare
      required: false
      type: string
      sets: [ "MetaData", "Alternate" ]

    - name: nar
      in: query
      description: Active region number
      required: false
      type: string
      sets: [ "Alternates" ]

paths:
  /test/:
    get:
      parameters:
        - $ref: "#/parameters/event_type"             <<<< $ref still works
        - $ref: "#/parameters/appearance_date"
      parameterSets: [ "MetaData", "Alternate" ] <<< yields id, time_start and nar
  /other/:
    get:
      parameters:
      parameterSets: [ "Alternate" ]         <<< yields time_start and nar

This is analogous to tags, but I used "sets" to avoid ambiguity. It's basically a shorthand for multiple $ref, but I think would be easier to read, maintain and reuse. It would not require changing the structure of parameters as in the OP, and would allow parameters to be in multiple sets, which the OP proposal does not allow (I think).

(I don't think there is a UI aspect to these sets, so no set descriptions are needed. Syntax for describing parameter sets would be added for documentation purposes, though.)

k-omukai commented 8 years ago

+1

fehguy commented 8 years ago

@OAI/tdc propose closing this with no action. The fix in OAI/OpenAPI-Specification#633 will help with this

webron commented 8 years ago

I think we still need to support this, and should be fairly easy.

OAI/OpenAPI-Specification#633 doesn't really help with this. This is not about global parameters, and reusable parameters existed in 2.0 already.

DavidBiesack commented 8 years ago

FWIW, I would like OAS to use structural abstractions (such as the tags I suggest above) over $ref representation "tricks" which IMHO hide or obscure the intent. The API designer's goal is to reuse common API constructs such as sets of parameters. Implementing that through $ref and JSON schema inclusion works, but doing so adds a level of redirection (which to me equates to obfuscation.)

earth2marsh commented 8 years ago

I agree with @webron that sets deserve consideration on their own.

ralusek commented 8 years ago

This should absolutely be a feature. For API endpoints with many shared configuration parameters, it is a huge hassle to maintain those same params everywhere.

rochifebo commented 8 years ago

+ 1 this would save me time and lines

jackkong commented 8 years ago

+1

matiman commented 8 years ago

+1

antoniogarcez commented 7 years ago

+1

dnascimento commented 7 years ago

+1

chenziliang commented 7 years ago

+1

ePaul commented 7 years ago

As my previous note seems to not be followed anymore, here a repetition:

Just as a note for everyone wanting to vote this up: Github recently added a feature allowing a "reaction" to each post, using the button in the top left corner of each post. This will not clutter the thread with contentless comments.

Please don't add a comment with just "+1", instead just upvote the top post. Just post a new comment if you actually are adding any content.

candlejack297 commented 7 years ago

Parameter sets seem better than the original proposal. Pretty simple QoL change that probably doesn't play badly with other proposals.

evgeny-l commented 7 years ago

+1

xor-gate commented 7 years ago

Grouping makes it much more clean, when having multiple endpoints with multiple parameters.

xelber commented 7 years ago

Would save quite a few lines of code and can focus on the specifics in an operation. Must have.

webron commented 7 years ago

Convenience is not a 'must have'. Not saying we're not in favor of supporting it, but keep it in proportion.

ndurchx commented 7 years ago

+1

narayan24 commented 7 years ago

+1

nidhindn commented 7 years ago

We strongly recommend this feature. For example consider HMAC auth headers. If we could group the parameters then we could use them with a single ref, now I have to use many small refs. I believe if grouping of definfitions like allOf exists then for the same reason grouping of parameters must also exist.

fehguy commented 7 years ago

We're past feature freeze for 3.0.0, so expect this isn't happening initially.

ghost commented 7 years ago

+1

chorsnell commented 7 years ago

2 years later and this still isn't a thing? any idea if this is going to be adopted?

Seems like plenty of positive reactions :)

@cansik did you ever get a more elegant solution than https://github.com/OAI/Overlay-Specification/issues/34? Or did you go the route @dolmen suggested with https://github.com/OAI/Overlay-Specification/issues/34

darrelmiller commented 7 years ago

This is definitely something we would like to do. The big question is what is the best way of doing it. This was my thought experiment on the subject https://github.com/OAI/Gluecon-Workshop/blob/master/Experiments/GroupRef.yml

chorsnell commented 7 years ago

@darrelmiller looks good. anything that promotes reuse and grouping of commom elements where ever possible has got to be a priority. Your implementation definitely seems fairly flexible, I will have a think on it and see if I have any further suggestions.

In API design I tend to favour having parameters rather than relying on a body object + schema definition so I can have better descriptions, examples and easier access to those properties from the swagger UI (have had a look around for best practises / knowledge on this but couldn't find much).

This means on resources with GET / PUT / POST, they usually end up exposing the same, or mostly the same params. Then this duplicated across various resources can get pretty duplicate heavy unless doing something like @cansik does here - https://github.com/OAI/Overlay-Specification/issues/34

cansik commented 7 years ago

@poppahorse As you mentioned, the issue is two years old. I have to admit that don not work with Swagger anymore, but I still think it is an important topic.

To solve the problem in our project, we decided to write a preprocessor (as @dolmen suggested) and to inject the parameters on startup. But I do not think that this is the right way to go. Because, as you see, there are a lot of people who would like to have a in-framework solution for that problem.

So, instead of everybody writing their own isolated tools, it would be better to have it directly in the specification.

But at the moment I do not know, what is needed to go a step further. Maybe the discussion about how it really could be implemented, should be opened again.

wzrdtales commented 7 years ago

@cansik When you moved away from Swagger, where did you move to?

At the current state swagger generates more work than it solves and is just not flexible enough. Many things are missing including this one from this issue. So I really don't want to use it anymore...

darrelmiller commented 7 years ago

@cansik @wzrdtales Many of the improvements in V3 of the spec came from people creating issues that proposed solutions to problems. Discussing how important things are and how much is missing, doesn't really move us forward. Proposing a concrete solution or commenting on solutions proposed by others does. The next version of the spec will most likely be a 3.1 and will come much quicker than 3.0 did.