Swagger/OpenAPI - use $ref to pass reusable defined parameters


brandonscript:

Suppose I have a similar parameter limit. This place is used everywhere, and if I need to update it, it's a pain to have to change it everywhere:

parameters:
    - name: limit
      in: query
      description: Limits the number of returned results
      required: false
      type: number
      format: int32

Can I use $ref to define it elsewhere and make it reusable? I came across this ticket suggesting that someone wants to change or improve the functionality, but I don't know if it already exists?

Ron:

This feature already exists in Swagger 2.0. The linked ticket discusses some of its specific mechanics, but doesn't affect the functionality of this feature.

On the top-level object (called the Swagger object), there is a parametersproperty where you can define reusable parameters. You can give the parameter any name and refer to it from the path/specific action. Top-level parameters are only definitions and do not automatically apply to all operations in the specification.

You can find an example here - https://github.com/swagger-api/swagger-spec/blob/master/fixtures/v2.0/json/resources/reusableParameters.json - even with restricted parameters.

In your case you want to do this:

# define a path with parameter reference
/path:
   get:
      parameters:
         - $ref: "#/parameters/limitParam"
         - $ref: "#/parameters/offsetParam"

# define reusable parameters:
parameters:
   limitParam:
      name: limit
      in: query
      description: Limits the number of returned results
      required: false
      type: integer
      format: int32
   offsetParam:
      name: offset
      in: query
      description: Offset from which start returned results
      required: false
      type: integer
      format: int32

Related


Swagger/OpenAPI - use $ref to pass reusable defined parameters

brandonscript: Suppose I have a similar parameter limit. This place is used everywhere, and if I need to update it, it's a pain to have to change it everywhere: parameters: - name: limit in: query description: Limits the number of returned resu

Swagger/OpenAPI - use $ref to pass reusable defined parameters

brandonscript: Suppose I have a similar parameter limit. This place is used everywhere, and if I need to update it, it's a pain to have to change it everywhere: parameters: - name: limit in: query description: Limits the number of returned resu

Pass parameters to reusable expressions in Entity Framework

username I want to declare and reuse Expression with filter via variable y. In one method I have the following: Expression<Func<Item, int, bool>> exFilter = (x, y) => x.Item.Id == y; Further, in the code, I try to use the declared expression (exFilter) return

Pass parameters to reusable expressions in Entity Framework

username I want to declare and reuse Expression with filter via variable y. In one method I have the following: Expression<Func<Item, int, bool>> exFilter = (x, y) => x.Item.Id == y; Further, in the code, I try to use the declared expression (exFilter) return

Pass parameters to reusable expressions in Entity Framework

username I want to declare and reuse Expression with filter via variable y. In one method I have the following: Expression<Func<Item, int, bool>> exFilter = (x, y) => x.Item.Id == y; Further, in the code, I try to use the declared expression (exFilter) return

Use the ref keyword to pass values

megabytes After reading the MSDN article on the ref keyword , I am confused about what C# does when you use the ref keyword to pass a value type. The documentation states that ValueTypes are not boxed. My question is how does C# handle passing value types as r

Use #selector to pass parameters

nomadic fields I'm a beginner in Swift and trying to initialize a function via NotificationCenter. The observer in "ViewController.swift" calls the function reload: override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserve

Use #selector to pass parameters

nomadic fields I'm a beginner in Swift and trying to initialize a function via NotificationCenter. The observer in "ViewController.swift" calls the function reload: override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserve

Use #selector to pass parameters

nomadic fields I'm a beginner in Swift and trying to initialize a function via NotificationCenter. The observer in "ViewController.swift" calls the function reload: override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserve

Use subclasses to pass as parameters

white matter disease I want to send a class as a parameter to a constructor this, I am doing this CasinoDealersum in two different classes Gambler, so on the receiving end StandActionI have a constructor who is taking the sum of 2 performerparameters hand. The

Use | to pass parameters

English stone. I've recently started learning some OpenGL and I've seen that some functions used by freeGLUT only take one parameter, but it's like more values can be passed. For example, glutInitDisplayMode(unsigned int displayMode);I can see that it only acc

Use #selector to pass parameters

nomadic fields I'm a beginner in Swift and trying to initialize a function via NotificationCenter. The observer in "ViewController.swift" calls the function reload: override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserve

Use #selector to pass parameters

nomadic fields I'm a beginner in Swift and trying to initialize a function via NotificationCenter. The observer in "ViewController.swift" calls the function reload: override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserve

Use subclasses to pass as parameters

white matter disease I want to send a class as a parameter to a constructor this, I am doing this CasinoDealersum in two different classes Gambler, so on the receiving end StandActionI have a constructor who is taking the sum of 2 performerparameters hand. The

Use subclasses to pass as parameters

white matter disease I want to send a class as a parameter to a constructor this, I am doing this CasinoDealersum in two different classes Gambler, so on the receiving end StandActionI have a constructor who is taking the sum of 2 performerparameters hand. The

Use | to pass parameters

English stone. I've recently started learning some OpenGL and I've seen that some functions used by freeGLUT only take one parameter, but it's like more values can be passed. For example, glutInitDisplayMode(unsigned int displayMode);I can see that it only acc

Use parameters defined in other functions

Luca Di Mauro: I'm not an expert in defining functions, but I need to create a new function. I have a function where do I manually enter the query; where I set parameters like language and country; Then there is the last function where I should pass all these

Use parameters defined in other functions

Luca Di Mauro: I'm not an expert in defining functions, but I need to create a new function. I have a function where do I manually enter the query; where I set parameters like language and country; Then there is the last function where I should pass all these

Cannot use ref or out parameters in anonymous methods

Tagyoureit I have a problem with my code in c# if anyone can help with my problem. In a function I am parsing an Xml file and saving it into a struct. Then I try to retrieve some information from the struct with a specific node id and my code fails "Cannot use

Cannot use ref or out parameters in anonymous methods

Tagyoureit I have a problem with my code in c# if anyone can help with my problem. In a function I am parsing an Xml file and saving it into a struct. Then I try to retrieve some information from the struct with a specific node id and my code fails "Cannot use

Cannot use ref or out parameters in anonymous methods

Tagyoureit I have a problem with my code in c# if anyone can help with my problem. In a function I am parsing an Xml file and saving it into a struct. Then I try to retrieve some information from the struct with a specific node id and my code fails "Cannot use

Cannot use ref or out parameters in anonymous methods

Tagyoureit I have a problem with my code in c# if anyone can help with my problem. In a function I am parsing an Xml file and saving it into a struct. Then I try to retrieve some information from the struct with a specific node id and my code fails "Cannot use

Cannot use ref or out parameters in anonymous methods

Tagyoureit I have a problem with my code in c# if anyone can help with my problem. In a function I am parsing an Xml file and saving it into a struct. Then I try to retrieve some information from the struct with a specific node id and my code fails "Cannot use

Cannot use ref or out parameters in anonymous methods

Tagyoureit I have a problem with my code in c# if anyone can help with my problem. In a function I am parsing an Xml file and saving it into a struct. Then I try to retrieve some information from the struct with a specific node id and my code fails "Cannot use