Giter Club home page Giter Club logo

Comments (8)

JessePelton avatar JessePelton commented on June 6, 2024 1

That works! It's a little more verbose and less centrally controlled, but it's a viable workaround for me. I didn't even have to remove the name definition from the shared $ref definition. This is consistent with the Swagger spec, which says, "If there are conflicts between the referenced definition and this Path Item's definition, the behavior is undefined." Since I use the same name in both places, you can at least make the argument that there's no conflict. Leaving the name in the shared definition can therefore be used to ensure that the definition is not used with some other parameter name.

Thanks!

from play-swagger.

kailuowang avatar kailuowang commented on June 6, 2024

I think you have - $ref: "#/parameters/domain in your list of parameters, and json is complaining that it couldn't find the name element in it. Don't know what you trying achieve with that line

from play-swagger.

SattaiLanfear avatar SattaiLanfear commented on June 6, 2024

@kailuowang The Swagger Specification allows you to define parameters and several other structures for reuse. As my api requires that header at... basically every single endpoint, I'd like to avoid copy-pasting it every time.

For an example, I tweaked one of the sample Swagger yaml files to include the parameter:

swagger: '2.0'
info:
  version: 1.0.0
  title: PetStore on Heroku
  description: |
    **This example has a working backend hosted in Heroku**

    You can try all HTTP operation described in this Swagger spec.

    Find source code of this API [here](https://github.com/mohsen1/petstore-api)
host: petstore-api.herokuapp.com
basePath: /pet
schemes:
  - http
  - https
consumes:
  - application/json
  - text/xml
produces:
  - application/json
  - text/html
parameters:
  domain:
    name: "X-VirtualDomain"
    in: header
    description: The domain (account) you're attempting to interact with.
    required: true
    type: string
paths:
  /:
    get:
      parameters:
        - $ref: "#/parameters/domain"
        - name: limit
          in: query
          description: number of pets to return
          type: integer
          default: 11
          minimum: 11
          maximum: 10000
      responses:
        200:
          description:  List all pets
          schema:
            title: Pets
            type: array
            items:
              $ref: '#/definitions/Pet'
    post:
      parameters:
        - $ref: "#/parameters/domain"
        - name: pet
          in: body
          description: The pet JSON you want to post
          schema:
            $ref: '#/definitions/Pet'
          required: true
      responses:
        200:
          description: Make a new pet
    put:
      parameters:
        - $ref: "#/parameters/domain"
        - name: pet
          in: body
          description: The pet JSON you want to post
          schema:
            $ref: '#/definitions/Pet'
          required: true
      responses:
        200:
          description: Updates the pet
  /{petId}:
    get:
      parameters:
        - $ref: "#/parameters/domain"
        - name: petId
          in: path
          type: string
          description: ID of the pet
          required: true
      responses:
        200:
          description: Sends the pet with pet Id

definitions:
  Pet:
    type: object
    properties:
      name:
        type: string
      birthday:
        type: integer
        format: int32

If you copy-paste that into http://editor.swagger.io/#/ you can see that swagger accepts parameter references with the same effect as copy-pasting the parameter definition

from play-swagger.

kailuowang avatar kailuowang commented on June 6, 2024

I see. this is a bug at the play-swagger side, our json processing doesn't recognize such heterogeneous parameter list. In particular here is the code that failed

private def mergeByName(base: JsArray, toMerge: JsArray): JsArray = {
JsArray(base.value.map { bs
val name = (bs \ "name").as[String]
findByName(toMerge, name).fold(bs) { f bs.as[JsObject] deepMerge f }
} ++ toMerge.value.filter { tm
val name = (tm \ "name").as[String]
findByName(base, name).isEmpty
})
}

This code merges the parameter list json automatically generated by play-swagger according to your routes file and the parameter list json provided by comment or root swagger.yml.
I think we just need to add a special handling case for such $ref. and a test case?
Contribution will be greatly appreciated. 😃

from play-swagger.

JessePelton avatar JessePelton commented on June 6, 2024

The same problem exists for query parameters that are included in the function signature in the routes file. The operation ends up with a parameter definition generated from the signature in the routes file and another defined by the $ref. The Swagger UI seems to detect that there are two definitions for the same parameter and ignores the latter, but I think play-swagger should override the generated definition with the one specified by $ref. This means peering into the reference to obtain the parameter's name and in values.
I'm new to Scala, Swagger, and Play (and by implication, play-swagger), so I'm not quite sure how to proceed. I guess the first question is whether I should create a new issue.

from play-swagger.

kailuowang avatar kailuowang commented on June 6, 2024

@JessePelton your situation is a bit different, as you want to override one generated by play-swagger while the original issue is about adding a new one by $ref. I wonder if you add the $ref inside the name what happens?
like

- name: email
  $ref: "#/parameters/email"

and in the shared $ref definition you don't define the name.

If that doesn't work than we will have to change play-swagger to read base yaml before doing the merge, which would not be a trivial change as above.

from play-swagger.

JessePelton avatar JessePelton commented on June 6, 2024

Not so workable after all. https://editor.swagger.io/ reports that the generated documentation is invalid because of the additional properties alongside the $ref. Worse, this prevents the swagger-ui "Try it" feature from working.

So...should I create a new issue?

from play-swagger.

kailuowang avatar kailuowang commented on June 6, 2024

Yeah a new issue would be great. One solution would be having swagger automatically remove all other attributes after the merge between the comment yaml and the autogenerated yaml if it sees a $ref property.

from play-swagger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.