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

Example of Parameters object reuse #1029

Closed
SensibleWood opened this issue Apr 5, 2017 · 11 comments
Closed

Example of Parameters object reuse #1029

SensibleWood opened this issue Apr 5, 2017 · 11 comments
Labels
examples requests for more or better examples in the specification param serialization Issues related to parameter and/or header serialization

Comments

@SensibleWood
Copy link

Hello. I can't find an example in the draft specification of Parameters object reuse i.e. when encapsulated by a Component object.

Suggest one is added to the draft specification for clarity and ease-of-use.

Apologies if there's already an issue open for this.

@whitlockjc
Copy link
Member

Reusing any component, regardless of type (header, parameter, response, ...) is the same so if you've seen one example, you've seen them all. The way this works is you define the object in #/components/{type} and then where you want to reuse it, you use a Reference Object to point to it. So for example, I might describe my person parameter at #/components/parameters/person and I would reference it elsewhere via {"$ref": "#/components/parameters/person"}.

I'm not saying we shouldn't have an example, just wanting to help you figure this out now instead of waiting on a change.

@SensibleWood
Copy link
Author

SensibleWood commented Apr 5, 2017

Thanks. I understood how the $ref is expressed as there's plenty of examples. What I specifically wanted to know was when you implement the reuse in an parameter object. Is it like this?

get:
  parameters:
    - $ref: #/components/parameters/person
    - in: query
      name: another_random_parameter
      type: string

Cheers

@whitlockjc
Copy link
Member

Yes, that's exactly right.

@SensibleWood
Copy link
Author

Sweet. Thanks!

@whitlockjc
Copy link
Member

No problem. I'll keep this open to let the suggestion of having an explicit parameters reference example in the documentation get processed.

@ghost
Copy link

ghost commented Apr 11, 2017

IS this the right place to ask about overrides of references? In the example above, person may be defined in: body, but I would like to to re-use it in: formData without defining multiple objects. The second declaration is discarded it it is in the reference. IF so, this would need to be documented, as it is the one I most frequently bump into stopping real re-use of parameters.

@whitlockjc
Copy link
Member

whitlockjc commented Apr 11, 2017

What you want to do isn't possible with one parameter definition because you can't use a reference and then override/replace/merge properties into it. Due to OAI not allowing JSON References everywhere, you will have some duplication somewhere. There are a few different ways to approach this, each with its own level of duplication. Below is one example:

components:
  schemas:
    Person:
      type: object
      properties:
        name:
          type: string
        age:
          type: integer
  parameters:
    FormPerson:
      name: person
      in: formData
      schema:
        $ref: '#/components/schemas/Person'
    BodyPerson:
      name: person
      in: body
      schema:
        $ref: '#/components/schemas/Person'

There are multiple ways to do reuse to help with this.

@ghost
Copy link

ghost commented Apr 12, 2017

Ah, I am only used to OAI2 spec without the standardised component section. Your form is indeed more lightweight, but only in OAI3. Good to know this is being addressed in the roadmap, thanks.

@whitlockjc
Copy link
Member

You could do the same with 2.0, just using #/parameters instead of #/components/parameters and #/definitions instead of #/components/schemas.

@handrews handrews added the $ref label Feb 24, 2020
@handrews handrews added examples requests for more or better examples in the specification re-use and removed $ref labels Jan 29, 2024
@handrews
Copy link
Member

The request to override parameter fields is being tracked as #2026.

This issue is only for tracking the originally requested example.

@handrews handrews added param serialization Issues related to parameter and/or header serialization and removed re-use labels Jan 31, 2024
@handrews
Copy link
Member

Given that we haven't seen more complaints about this, and the confusion was likely due to changes from the 2.0 timeframe, I'm going to close this. We can't add an example of every possible use of $ref, and (aside from Path Item, which has other issues tracking it) they're all the same anwyay. Please feel free to re-open / open a new issue if you object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples requests for more or better examples in the specification param serialization Issues related to parameter and/or header serialization
Projects
None yet
Development

No branches or pull requests

3 participants