Skip to content
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

Re-usable enums in path/query parameters #300

Closed
marcus-maws opened this issue Mar 5, 2015 · 27 comments
Closed

Re-usable enums in path/query parameters #300

marcus-maws opened this issue Mar 5, 2015 · 27 comments

Comments

@marcus-maws
Copy link

I've been told that it's not possible to use schemas in path/query parameters, but I have a case where multiple paths use the same enum pattern for inputs.

For example, I have the following pattern that is duplicated over many paths:

parameters:
- in: "query"
  ...
  enum: 
  - id
  - sku
  - guid
  - stock
  ...

Is it possible to generalize this in some way, so that I don't have to define them for each path, but can just reference them?

@webron
Copy link
Member

webron commented Mar 5, 2015

I think we've had a similar issue about it, or someone was talking about it in the mailing list. I think it's a fair request to avoid repetitions for 'primitive' definitions as well and it's something we should look into in the next version of the spec.

@marcus-maws
Copy link
Author

OK, thanks for the quick response.

@webron
Copy link
Member

webron commented Mar 5, 2015

No, don't close it :) It's good to get feedback from others as well.

@webron webron reopened this Mar 5, 2015
@marcus-maws
Copy link
Author

OK. :-)

@marcus-maws
Copy link
Author

On a separate issue (touched on in #290), I have a situation where multiple paths can take the same collection of parameters. It appears that again, I have to define all those parameters individually for each path. Being able to separate all those parameters would be useful as well.

@webron
Copy link
Member

webron commented Mar 5, 2015

That should be a separate issue though. However, you can define parameters in a single place and then refer to them from multiple paths.

@mohsen1
Copy link
Contributor

mohsen1 commented Mar 5, 2015

If you're writing you specs in YAML you can take advantage of YAML anchors to avoid reputations.

@webron
Copy link
Member

webron commented Mar 5, 2015

But since there's a solution for that in the spec, it's better to utilize than than to utilize a solution that's can't be converted back and forth between JSON and YAML.

@webron
Copy link
Member

webron commented Mar 5, 2015

My bad, I thought @mohsen1 referred to repeating the parameters. His solution is most certainly valid for the original request though!

@JamesMessinger
Copy link
Contributor

This is already doable, though perhaps not very intuitive:
https://github.com/BigstickCarpet/swagger-parser/blob/master/tests/files/non-object-refs.yaml#L40

@Ckim15
Copy link

Ckim15 commented Mar 7, 2015

@mohsen1, Could you provide a working sample yaml spec where I can avoid repetition for enum?

I checked BigStickCarpet's url (https://github.com/BigstickCarpet/swagger-parser/blob/master/tests/files/non-object-refs.yaml#L40), It doesn't work.

@mohsen1
Copy link
Contributor

mohsen1 commented Mar 7, 2015

@chanthakim15 Here is a quick example

---
swagger: '2.0'
info:
  version: 1.0.0
  title: Pets Store
paths:
  /pets:
    get:
      responses:
        200:
          description: Returns all the pets
          schema:
            $ref: '#/definitions/List'
        201:
          description: Returns all the pets
          schema:
            $ref: '#/definitions/Set'

definitions:
  List:
    type: string
    enum: &my-list
      - one
      - two
      - three
  Set:
    type: string
    enum: *my-list

@BigstickCarpet's example is correct. If any of swagger tools is not supporting it, it's a bug. Please file a bug for it.

@rdrey
Copy link

rdrey commented Aug 27, 2015

@webron
Copy link
Member

webron commented Aug 27, 2015

OMG, what a coincidence, I just answered on it there too! 😉

@rdrey
Copy link

rdrey commented Aug 27, 2015

:P Thanks! Got the YAML reference trick to work, only issue I had is that the order in the file seems to be important. (Sorry, I'm a YAML noob.) So now I have definitions above paths.

@webron
Copy link
Member

webron commented Aug 27, 2015

Actually, the order is meaningless.

@rdrey
Copy link

rdrey commented Aug 27, 2015

Unidentified alias "OperationType" at line 78, column 33: enum: *OperationType ^

Flip the order, all good

EDIT: Some more info: Using a local copy of the Swagger Editor. Did reproduce with http://editor.swagger.io/#/ as well, quickly

@webron
Copy link
Member

webron commented Aug 27, 2015

That sounds like a parsing bug.

@rdrey
Copy link

rdrey commented Aug 27, 2015

Would you like a minimal example to repro in here? Or should I open a new issue?

@webron
Copy link
Member

webron commented Aug 27, 2015

It's got nothing to do with swagger-spec. I'm not sure which project is giving you the error, but if you want to report it, you should report it on the right one.

@rdrey
Copy link

rdrey commented Aug 27, 2015

Makes sense, sorry. Will report to Swagger Editor, but not sure which parser they use internally.

@webron
Copy link
Member

webron commented Aug 27, 2015

Doesn't matter. @mohsen1 definitely knows more about it than me, so I could be mistaken, but better keep that information in swagger-editor. Thanks!

@gersongoulart
Copy link

gersongoulart commented Feb 16, 2016

+1 for this feature baked in Swagger. I think the "YAML reference" serves as good inspiration to how the Swagger schema could handle this... e.g.:

// (...)
"parameters": {
    "marketIds": {
        "name": "marketIds",
        "$ref": "#/definitions/idsParam"
    },
    "clientIds": {
        "name": "clientIds",
        "$ref": "#/definitions/idsParam"
    },
},
// (...)
"definitions": {
    "idsParam": {
        "in": "query",
        "required": false,
        "description": "One or more id integers as a comma delimited array",
        "type": "array",
        "items": {
              "type": "integer"
        }
},

@fehguy
Copy link
Contributor

fehguy commented Feb 16, 2016

Unfortunately $ref cannot have a peer in the json model, per JSON schema.

@webron
Copy link
Member

webron commented Feb 21, 2016

This is also covered by #301.

Parent issue: #565.

@webron
Copy link
Member

webron commented Jul 21, 2016

Resolved with #654.

@scara
Copy link

scara commented May 10, 2019

Will report to Swagger Editor, but not sure which parser they use internally.

For those still interested: swagger-api/swagger-editor#609 (comment).

HTH,
Matteo

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

9 participants