oliyh / martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
MIT License
525 stars 42 forks source link

Can't find a way to use $ref parameters #176

Closed saidone75 closed 5 months ago

saidone75 commented 1 year ago

Hi, I read all the relevant issues but I'm still unable to use $ref for common parameters, e.g.: openapi:

info:
components:
  parameters:
    offsetParam:  # <-- Arbitrary name for the definition that will be used to refer to it.
      # Not necessarily the same as the parameter name.
      in: query
      name: offset
      required: false
      schema:
        type: integer
        minimum: 0
      description: The number of items to skip before starting to collect the result set.
    limitParam:
      in: query
      name: limit
      required: false
      schema:
        type: integer
        minimum: 1
        maximum: 50
        default: 20
      description: The numbers of items to return.
paths:
  /users:
    get:
      operationId: getUsers
      summary: Gets a list of users.
      parameters:
        - $ref: '#/components/parameters/offsetParam'
        - $ref: '#/components/parameters/limitParam'
      responses:
        '200':
          description: OK

and this will produce:

(let [client (martian-http/bootstrap-openapi "openapi.yaml")]
    (martian/explore client :get-users))
=> {:summary "Gets a list of users.", :parameters {}, :returns {200 Any}

Am I missing something? Thanks, Marco

oliyh commented 1 year ago

Hi,

That should work, it's possibly a missing implementation. I will try to find time to look at this (PRs also welcome).

Thanks!

RafaeLeal commented 5 months ago

I've tried to reproduce it to understand if it was the same issue I was having (https://github.com/oliyh/martian/issues/195) But I discovered that since you're using components, you are using spec from OpenAPI v3, so adding openapi: "3.1.0" solves the issue for you @saidone75

At least for me:

openapi: "3.0.1"
info:
components:
  parameters:
    offsetParam:  # <-- Arbitrary name for the definition that will be used to refer to it.
      # Not necessarily the same as the parameter name.
      in: query
      name: offset
      required: false
      schema:
        type: integer
        minimum: 0
      description: The number of items to skip before starting to collect the result set.
    limitParam:
      in: query
      name: limit
      required: false
      schema:
        type: integer
        minimum: 1
        maximum: 50
        default: 20
      description: The numbers of items to return.
paths:
  /users:
    get:
      operationId: getUsers
      summary: Gets a list of users.
      parameters:
        - $ref: '#/components/parameters/offsetParam'
        - $ref: '#/components/parameters/limitParam'
      responses:
        '200':
          description: OK

then run bootstrap, works fine.

(let [client (martian-httpkit/bootstrap-openapi "/tmp/openapiv3-example.yaml")]
  (martian/explore client :get-users))
=>  {:summary "Gets a list of users.", :parameters {#schema.core.OptionalKey{:k :offset} Int, #schema.core.OptionalKey{:k :limit} (default Int 20)}, :returns {200 nil}}

I'd close this issue.

oliyh commented 5 months ago

Thank you @RafaeLeal for the investigation and help