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

[Question] Extend/override properties of a parameter #2026

Open
emauricio opened this issue Oct 4, 2019 · 56 comments
Open

[Question] Extend/override properties of a parameter #2026

emauricio opened this issue Oct 4, 2019 · 56 comments
Labels
re-use: traits/merges Selective or modified re-use

Comments

@emauricio
Copy link

Hello there, I got a small question about the compoments[parameter]

Currently, im trying to make some parameters reusable and the basic seems pretty simple.

# 
openapi: 3.0.1
info:
  title: An include file to define common parameters
  version: 1.0.0
paths: 
    /test:
      get:
        ...
        parameters:
          - $ref: 'parameters.yml#/components/parameters/reusableParam'
        ...
components:
  parameters:
    reusableParam:
      in: query
      name: reusableParam
      description: filter something
      schema:
        type: number
        default: 30

Now my question is, how can I avoid to duplicate the reusableParam if another path might need the same one but maybe with required: true or different default: 50

what would be the "correct" way to do it?

Thank you in advance.

@handrews handrews added the $ref label Feb 24, 2020
@emauricio emauricio changed the title [Question] Extend/overrideproperties of a parameter [Question] Extend/override properties of a parameter Feb 25, 2020
@emauricio
Copy link
Author

Any update about this?
It is quite funny that I end up in my own ticket some months later looking for the same. 😄

@bilak
Copy link

bilak commented Apr 14, 2020

This would be very helpful. I'm also defining parameters which differ only with required attribute (true
|false).

@geoffreywiseman
Copy link

I have the exact same schema I need to use in three places, I just want to change the top-level description. The schema's the same, the description varies just slightly in ways that are subtly important. So instead of re-using it, I have to duplicate it.

@MikeRalphson
Copy link
Member

@geoffreywiseman OAS (or whatever version the next release ends up as being) will support $ref with a sibling description property.

@geoffreywiseman
Copy link

OK, now I'm curious about why the next version might not be OAS. ;) Is there somewhere I go to read about that?

@MikeRalphson
Copy link
Member

Sorry, there should have been a 3.1 in there, which got lost in editing!

@gfiehler
Copy link

I agree and also need this feature, but with the addition of wanting to override the example values on my parameters to be more relevant to each operation. Or the ability to do some type of allOf on parameters where I can include an existing parameter definition and override one or more properties as you can with schemas.

@colin-p-hill
Copy link

Also running into a use for this. I've got parameters that are reused across multiple endpoints, but they're sometimes query parameters and sometimes path parameters, depending on whether they're identifying a single resource or filtering a collection of resources.

@osteel
Copy link

osteel commented Dec 8, 2020

Yes, same need here. Also not the first time I end up on this thread 😄

So much duplication going on in my OpenAPI definitions, at the moment.

@bansal6432
Copy link

I am also feeling the need for the same.
Need to override name and required property for a path parameter.

@danielesegato
Copy link

danielesegato commented Mar 18, 2021

I've another use-case for this

No reuse:

openapi: 3.0.0
components:
  schemas:
    TextContainer:
      type: object
      properties:
        format:
          title: Text Format
          description: Type of formatting of this text.
          type: string
          enum:
          - plain
          - html
          - markdown
          default: plain
        text:
          type: string
      required:
      - text

paths:
  /find:
    get:
      summary: FindTexts
      description: Find specific texts
      parameters:
        - name: q
          in: query
          required: true
          schema:
            type: string
        - name: only
          in: query
          required: false
          schema:
            description: Optionally filter a specific text type.
            type: string
            enum:
              - plain
              - html
              - markdown
              - any
            default: any

3 different features I'd like here, please note that:

  1. the two usages have different description
  2. the two usages have different default
  3. the second usage has an additional property "any" compared to the first usage

The way I would like it to work:

openapi: 3.0.0
components:
  schemas:
    TextFormat:
      title: Text Format
      description: Type of formatting of this text.
      type: string
      enum:
        - plain
        - html
        - markdown
    TextContainer:
      type: object
      properties:
        format:
          $ref: '#/components/schemas/TextFormat'
          default: plain
        text:
          type: string
      required:
      - text

paths:
  /find:
    get:
      summary: FindTexts
      description: Find specific texts
      parameters:
        - name: q
          in: query
          required: true
          schema:
            type: string
        - name: only
          in: query
          required: false
          schema:
            $ref: '#/components/schemas/TextFormat'
            // override
            description: Optionally filter a specific text type.
            enum:
              // some way to tell I want to add to the enum (a similar argument could be done for exclude)
              - $add: [any]
            default: any

@EmhyrVarEmreis
Copy link

Allowing to reuse referenced params with option to override "required" flag would be awsome

@Ramesh-X
Copy link

Ramesh-X commented May 6, 2021

Is this going to be available in v3.1 ?

@MikeRalphson
Copy link
Member

Is this going to be available in v3.1 ?

3.1.0 is already out. You can override the description of a parameterObject in a $ref, but not (yet) the required field.

@mahnunchik
Copy link

Any news about overriding required?

@schmurfy
Copy link

Why not allow overriding everything instead of field by field ?
Here is what I would like to override:

  • type: since in some case the param could be null and in others not (ex: type: ["object", "null"])
  • writeOnly & readOnly: same reason as above
  • deprecated

it looks like for now $ref is just not doing the job for me unless I missed something

@tmtron
Copy link

tmtron commented Dec 13, 2021

We'd also like to override example/examples. We often have endpoints that return the same object, but the examples should be different

see also: nestjs/swagger#1723

@axl8713
Copy link

axl8713 commented Feb 10, 2022

As the Swagger Editor suggests, one could wrap the $ref into allOff to extend or override its properties:

parameters:
  - allOf:
      - $ref: '#/components/parameters/filters'
      - example: "overridden example"
  - $ref: '#/components/parameters/fields'

See https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof

@hkosova
Copy link
Contributor

hkosova commented Feb 10, 2022

@axl8713 your example is unfortunately not a valid construct in OpenAPI.

parameters:
  - allOf:
      - $ref: '#/components/parameters/filters'
      - example: "overridden example"
  - $ref: '#/components/parameters/fields'

@axl8713
Copy link

axl8713 commented Feb 10, 2022

@axl8713 your example is unfortunately not a valid construct in OpenAPI.

parameters:
  - allOf:
      - $ref: '#/components/parameters/filters'
      - example: "overridden example"
  - $ref: '#/components/parameters/fields'

I've got misleaded by the Swagger Editor then, where it seems at least to be rendered correctly (although with some structural errors).

@billytetrud
Copy link

Having the same issues. Would very much want a consistent way to override properties. Others have mentioned examples of properties that basically need this functionality, but I don't see why the spec should pick and choose what properties are overridable.

@Midorina
Copy link

Midorina commented May 3, 2022

This, please.

@karenetheridge
Copy link
Member

Anything below the schema keyword (which is about half of the requests in this thread) is not covered by the OpenAPI specification, but by JSON Schema. Please start a discussion at https://github.com/json-schema-org/community/discussions and we can help find a solution for you.

@darrelmiller
Copy link
Member

darrelmiller commented Apr 1, 2023

To qualify, I am not saying people should not use aliases to help them create OAS descriptions efficiently. We fully support folks who start with some other format and use tooling to create their OAS description. If that tooling is a yaml parser that can dereference aliases, then all the power to you.
I am just saying it should not be EXPECTED that OAS compliant tooling will dereference aliases for you.

@MikeRalphson
Copy link
Member

@darrelmiller which YAML parsers don't support aliases? 😁 Any OAS tool using a YAML parser is (almost certainly) going to support aliases whether we like it or not. (oas-kit checks the loaded object representation afterwards and warns by default on the use of aliases).

@darrelmiller
Copy link
Member

darrelmiller commented Apr 1, 2023

@MikeRalphson I don't know. Are there YAML parsers that will enforce the JSON Schema ruleset, and if so will it still process aliases?

What if I rephrased and said that users should not expect OAS tooling to preserve aliases after parsing?

Which leads us to another conversation... I don't think application/openapi+yaml should declare support for aliases in fragment identifiers.

@MikeRalphson
Copy link
Member

MikeRalphson commented Apr 1, 2023

@darrelmiller I'll test with @eemeli's yaml implementation - it is very compliant with the YAML specification.

I'm about 75% minded the other way, in that we should allow alias fragments in apolication/openapi+yaml

@darrelmiller
Copy link
Member

@MikeRalphson You're just a sucker for punishment ;-)

@ioggstream
Copy link
Contributor

As long as the resulting document is valid OAS, limiting YAML syntax can be problematic and barely enforceable.

@MikeRalphson
Copy link
Member

@darrelmiller I've tested with @eemeli's yaml implementation and it parses YAML aliases with the core and failsafe schemas, but not the json schema. So I think we're back to the question of what @OAI/tsc's intended ruling is on YAML aliases in OpenAPI documents, and whether we need language specifically about aliases, as they are neither tags nor mapping keys.

@eemeli
Copy link
Contributor

eemeli commented Apr 3, 2023

@darrelmiller I've tested with @eemeli's yaml implementation and it parses YAML aliases with the core and failsafe schemas, but not the json schema. [...]

Anchors and aliases are a YAML feature that's independent of the schema. The following works for me:

import { parse } from 'yaml'

const src = `
"a": &a 42
"b": *a`

parse(src, { schema: 'core' }) // { a: 42, b: 42 }
parse(src, { schema: 'failsafe' }) // { a: '42', b: '42' }
parse(src, { schema: 'json' }) // { a: 42, b: 42 }

In your testing, you may have hit a stumbling block on string scalars needing to be quoted when using the json schema.

@MikeRalphson
Copy link
Member

In your testing, you may have hit a stumbling block on string scalars needing to be quoted when using the json schema.

Doh! I'm sure that's it, thank you. Glad I tagged you now.

@MikeRalphson
Copy link
Member

@darrelmiller Confirmed. You can use aliases in the JSON ruleset/schema if you quote your keys and values.

@fredericjaume-oc
Copy link

It would be great to be able to override example values. We have an in-house API testing framework that uses the examples to run some tests, and in some cases multiple endpoints sharing the same path parameter can't be tested with the same example value.

@zzafarr
Copy link

zzafarr commented Jan 19, 2024

It's Jan 2024, still no solution ( > 4 years ) ?
It is totally makes sense that same type is/can be reused across in different schemas with different description, default values, is required, custom extensions etc ..
Is this on the road map?
What is the recommended workaround fir a time being?
Thank you

@handrews
Copy link
Member

handrews commented Jan 19, 2024

@zzafarr it is addressed by the Moonwalk (OAS 4) proposal - I'm pretty certain that the amount of change required can only be done in a major version update. I have also filed #3508 to figure out how to clarify this sort of thing, as there are a lot of open issues in this repo that look abandoned but are actually being actively worked on in the Moonwalk repo.

[EDIT: There's no guarantee that every aspect of this will be solved by Moonwalk, but it's where the issue is being considered, and I'm confident that there will be some improvements in this area.]

@ChihweiLHBird
Copy link

ChihweiLHBird commented May 16, 2024

Hi @handrews, thank you for the info! I just read the Moonwalk proposal, and I couldn't find a way to override the values of a contentSchema in a request. For example, required field in the JSONSchema (of contentSchema) can not be overridden. Do you think that was a part of this issue that's not resolved by the Moonwalk proposal? Or is there any way to do it but I missed?

@handrews
Copy link
Member

@ChihweiLHBird the reason you couldn't find it is that it's not there, at least not yet, and it's not clear to us how to best address that use case.

With the scope of JSON Schema, well.. it's a constraint system and requires a very different sort of organization for re-use than (for example) strongly typed OO languages like Java.

In the larger scope of OpenAPI, the proposed Overlay Specification is one idea for doing such edits at a higher level, which would target 3.x.

For Moonwalk, we just don't know how it's all going to fit together yet. Is JSON Schema even the right technology for all of this? Should we use more JSON Schema? Less JSON Schema? Make it an option to replace it with something else? All of this is being debated in-depth, and it's not clear where we will land yet. The mismatch between JSON Schema as a constraint system and the needs of code generators is one of the most difficult topics we're working through.

@9ao9ai9ar
Copy link

Can't this be done using $dynamicAnchor/$dynamicRef? How do I override example for specific properties in components in OpenAPI?. Though even in 2024, I've yet to find tools that support these constructs... and you're planning to ship out OpenAPI 4 by the end of this year? Good luck with that. But please make sure to follow semantic versioning from this point onwards and write better documentation making it clear what features/vocabularies belong to what specifications and versions (e.g. JSON Schema 2020-12, YAML 1.2, etc.). It's very confusing for beginners when they first encounter "anchors" (JSON Schema anchors or YAML anchors?), or read articles describing OpenAPI as a "superset" of JSON Schema, but didn't actually support using $id and $schema in the OpenAPI documents.

@handrews
Copy link
Member

@9ao9ai9ar as you can see in #3528 and #3529, we do intend to stick with semantic versioning. 3.1 was a very complex situation with no good option for compatibility, but it was also for a very specific reason which is not going to be a problem in the future.

3.0.4 and 3.1.1 should be out within a couple of months, and there will be a 3.2 (strictly compatible with 3.1, and with a small amount of enhancements that should make it fairly quick to implement) before there will be a 4.0. We are looking at trying out 4.0 ideas in the 3.x series, so it's possible that shipping "Moonwalk" in 2024 will actually look like shipping incremental 3.x releases containing Moonwalk ideas rather than shipping a full 4.0 within the next few months. It's very up in the air as of this week.

Some things in 3.0.4 and 3.1.1 are specifically about clarifying requirements that come from other specificaitons on which the OAS depends.

BTW, $id and $schema are supported in 3.1 OpenAPI documents. If a tool does not support them, you should lobby the tooling vendor.

@handrews
Copy link
Member

@9ao9ai9ar I forgot to answer your initial question - yes, at least some use cases can be handled by $dynamicRef and $dynamicAnchor. In 3.1.1, we've added a section on how this construct can be used for generic types. It's our hope that calling out a clear use case will motivate more tooling vendors to support it, especially because support for generic types has been requeseted many times (there were at least 5 open issues for it that I closed with 3.1.1).

@9ao9ai9ar
Copy link

9ao9ai9ar commented Jul 16, 2024

@handrews Looking forward to the 3.0.4 and 3.1.1 publications! I like the idea of doing incremental releases; even though OpenAPI still feels lacking in some areas, I think what we currently have in 3.1, if fully supported by available tools and documented clearly, is powerful enough to meet most requirements.

About $id and $schema, thank you for confirming that they are supported in OpenAPI 3.1, but it'll take some time for outdated information to decay into oblivion :). I only meantioned them because they appear in most examples using $dynamicRef/$dynamicAnchor, and I think no one has explained the background necessary for understanding these features better than Juan Cruz Viotti's blog post.

I also have a need for generic types for a personal project I'm working on right now. Sadly, the tool I use is awesome in every way except not supporting $dynamicRef/$dynamicAnchor and YAML merge keys, the only two methods I know that should be able to achieve my goal, but fortunately, the lack of support is not really a blocker for me.

@guizoxxv
Copy link

guizoxxv commented Aug 14, 2024

I believe you could do:

openapi: 3.0.1
info:
  title: An include file to define common parameters
  version: 1.0.0
paths: 
    /test:
      get:
        parameters:
          - $ref: 'reusableParam.yaml'
        responses:
          200:
            description: OK
    /test-2:
      get:
        parameters:
          - name: reusableParam
            allOf:
              - $ref: 'reusableParam.yaml'
              - required: true
              - schema:
                type: number
                default: 50
        responses:
          200:
            description: OK

And in another file reusableParam.yaml:

name: reusableParam
in: query
description: filter something
schema:
  type: number
  default: 30

@handrews
Copy link
Member

@guizoxxv allOf is a JSON Schema keyword and only usable inside of a Schema Object

@guizoxxv
Copy link

guizoxxv commented Aug 15, 2024

@guizoxxv allOf is a JSON Schema keyword and only usable inside of a Schema Object

@handrews It seems to work when I run npm run dev on a local file in swagger-ui, but indeed it does not work when I try it in https://editor.swagger.io/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
re-use: traits/merges Selective or modified re-use
Projects
None yet
Development

No branches or pull requests