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

Multiple components blocks #18

Closed
Amnesthesia opened this issue Jun 18, 2018 · 6 comments
Closed

Multiple components blocks #18

Amnesthesia opened this issue Jun 18, 2018 · 6 comments

Comments

@Amnesthesia
Copy link

I want to separate components into multiple files without duplicating them, so I have some modules that define response_refs and param_refs, and then I include these helpers.

It seems like only the last components do ... end will be evaluated, and all the ones before will be overwritten.

Is there a way I can define components in multiple files?

@zhandao
Copy link
Owner

zhandao commented Jun 19, 2018

Sorry, it seems that I can't completely reproduce your problem, could you provide more detailed information?

@Amnesthesia
Copy link
Author

Amnesthesia commented Jun 19, 2018

For example, if you have something like this:

module Version1::OpenApi::ModelAResponses
   def self.included(base)
      base.components do
           response :SingleModelA => ['success', :json, data: {
              field: { type: String, desc: 'Test' }
           }]

          response :ListModelA => ['success', :json, data: {
              data: [{ field: { type: String, desc: 'Test' } }]
           }]
       end
   end
end

and another module

module Version1::OpenApi::ModelBResponses
   def self.included(base)
      base.components do
           response :SingleModelB => ['success', :json, data: {
              field: { type: String, desc: 'Test' }
           }]

           response :ListModelB => ['success', :json, data: {
              data: [{ field: { type: String, desc: 'Test' } }]
           }]
       end
   end
end

and in your BaseController, you do something like:

class Version1::BaseController < ApplicationController
    Version1::OpenApi::ModelAResponses
    Version1::OpenApi::ModelBResponses
end

This will not work, :ListModelA will not be defined because Version1::OpenApi::ModelAResponses was not the last included module, and Version1::OpenApi::ModelBResponses redefines components

class Version1::ModelAController < Version1::BaseController

   api :index, 'List model A' do
       response_ref 200 => :ListModelA # This does not exist now
   end
   def index
      # ....
   end
end

But this will work:

class Version1::ModelBController < Version1::BaseController

   api :index, 'List model B' do
       response_ref 200 => :ListModelB # This exists
   end
   def index
      # ....
   end
end

The .json schema generated will only contain components defined in Version1::OpenApi::ModelBResponses or whatever is the last included module.

The only way I can get it to work is to put all responses and all parameter components in the same component block, if you have more than 1 component block, only the last component block will be used

@zhandao
Copy link
Owner

zhandao commented Jun 19, 2018

Please update this gem and try again. 😄

@zhandao
Copy link
Owner

zhandao commented Jun 20, 2018

Is there any problem?

@Amnesthesia
Copy link
Author

Thank you @zhandao! I will try it out tonight and tell you how it went :-)

Appreciate the quick response!

@zhandao
Copy link
Owner

zhandao commented Jun 22, 2018

Hi, should I finish this issue? @Amnesthesia

@zhandao zhandao closed this as completed Jul 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants