Search code examples
ruby-on-railsswagger-uirswag

How to switch the order of two keys in OpenAPI YAML generated by Rswag?


In Swagger UI, there's a bug with allOf and nested associations which causes those parts of the schemas to not be included:

The solution is to put paths: before components: in your OpenAPI YAML. I tested it, and this works.

So, how can I get Rswag to generate in this order whenever I run rails rswag?

I would be fine with just modifying the file immediately after generation. But I need it to be generated in this order for things to work


Solution

  • Thanks to @Helen for recommending this approach:

    # config/initializers/rswag_api.rb
    
    Rswag::Api.configure do |c|
      c.swagger_filter = lambda { |swagger, env| 
        # Remove the 'components' key and store its value
        components = swagger.delete('components')
    
        # Reinsert the 'components' key at the end
        swagger['components'] = components
      }
    end
    

    This solution works despite not changing the yml. So it addresses the core issue which is in Swagger UI. Thanks for the help.