livingsocial / swagger_yard

Swagger-UI compliant JSON generated from YARD. For RESTful Rails apps.
MIT License
51 stars 28 forks source link

Add @!model directive to declare model without an associated class #66

Closed Quiwin closed 4 years ago

Quiwin commented 4 years ago

Hello,

Really appreciate this project! The goal of this pull request is to add a way to create model without the need of a matching class. For example it is useful when some fields are required in POST but not in PATCH.

This directive generate a @model tag with a dummy class taking the name given to the directive. All tags and docstring given to the directive are passed to the generated @model.

Also update ruby versions tested and remove the specification of patch to keep them up-to-date. Furthermore I removed the .ruby-version file since its not really needed in a library, what is your take on it?

Quiwin commented 4 years ago

We could add also add a list of os in travis to test against linux and osx if you are ok with that.

nicksieger commented 4 years ago

Love this, will release a 1.0.3 shortly! Thanks for the contribution!

mengkuan commented 4 years ago

@Quiwin - Thanks for providing this functionality! Was trying to use @example tags with a @!model declared class but couldn't get the specs to show the examples.

Appreciate any tips on how the following can be achieved:

# @!model CreatePet
# @property id(required)    [integer]
# @example id
#   3
# @property name(required)  [string]
# @example name
#   "Test"
Quiwin commented 4 years ago

Hello @mengkuan - I'm sorry but i'm not able to reproduce it. Your example generates this json for me:

  "components": {
    "schemas": {
      "CreatePet": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "example": 3
          },
          "name": {
            "type": "string",
            "example": "Test"
          }
        },
        "required": [
          "id",
          "name"
        ]
      }
    },
    "securitySchemes": {
    }
  }

If you have more informations I will be happy to investigate!

mengkuan commented 4 years ago

Hi @Quiwin - Apologies for the false alarm. I had that within a controller previously and it didn't work. After taking it out of the controller it worked immediately. Thanks for investigating!

mraerino commented 4 years ago

i'm also having a hard time getting this to work.

can you tell me where it should be located so it's being picked up? i tried controller and serializer, but no luck yet

mengkuan commented 4 years ago

@mraerino - Not sure if this is helpful for you, I realized later on that in my case I had the @!model declared within a controller. In order for that to be picked up by swagger_yard, you have to make swagger_yard look at controller classes as well for model declarations in swagger_yard's initializer. For instance, something like this works for me:

    config.model_path = [::Rails.root + 'app/models/**/*',
                         ::Rails.root + 'app/controllers/**/*']
nicksieger commented 4 years ago

@mengkuan @mraerino If either of you can suggest README updates that would make this clearer, let me know. Even better, would appreciate a PR!