Class: RailsOpenapiGen::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-openapi-gen.rb

Instance Method Summary collapse

Instance Method Details

#runvoid

This method returns an undefined value.

Runs the OpenAPI generation process



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rails-openapi-gen.rb', line 72

def run
  # Load configuration
  RailsOpenapiGen.configuration.load_from_file

  routes = Parsers::RoutesParser.new.parse

  # Debug: Show filtering configuration
  config = RailsOpenapiGen.configuration

  # Apply filtering with debug output
  filtered_routes = routes.select do |route|
    included = config.route_included?(route[:path])
    included
  end

  schemas = {}
  all_components = {}

  filtered_routes.each do |route|
    controller_info = Parsers::ControllerParser.new(route).parse

    next unless controller_info[:jbuilder_path]

    jbuilder_parser = Parsers::Jbuilder::JbuilderParser.new(controller_info[:jbuilder_path])
    ast_node = jbuilder_parser.parse

    # Collect components from this parser
    if ENV['RAILS_OPENAPI_DEBUG']
      puts "🔍 DEBUG: jbuilder_parser has ast_parser: #{jbuilder_parser.respond_to?(:ast_parser)}"
      puts "🔍 DEBUG: ast_parser is nil: #{jbuilder_parser.ast_parser.nil?}" if jbuilder_parser.respond_to?(:ast_parser)
      if jbuilder_parser.respond_to?(:ast_parser) && jbuilder_parser.ast_parser && jbuilder_parser.ast_parser.respond_to?(:partial_components)
        puts "🔍 DEBUG: partial_components count: #{jbuilder_parser.ast_parser.partial_components.size}"
      end
    end

    if jbuilder_parser.respond_to?(:ast_parser) &&
       jbuilder_parser.ast_parser &&
       jbuilder_parser.ast_parser.respond_to?(:partial_components) &&
       jbuilder_parser.ast_parser.partial_components.any?
      puts "📦 Merging #{jbuilder_parser.ast_parser.partial_components.size} components" if ENV['RAILS_OPENAPI_DEBUG']
      all_components.merge!(jbuilder_parser.ast_parser.partial_components)
    end

    schema = Processors::AstToSchemaProcessor.new.process_to_schema(ast_node)
    schemas[route] = {
      schema: schema,
      parameters: controller_info[:parameters] || {},
      operation: {} # Operation data is not available in AST approach
    }
  rescue StandardError => e
    puts "❌ ERROR processing route #{route[:method]} #{route[:path]}: #{e.class} - #{e.message}" if ENV['RAILS_OPENAPI_DEBUG']
    puts "❌ ERROR at: #{e.backtrace.first(3).join("\n")}" if ENV['RAILS_OPENAPI_DEBUG']
    raise
  end

  Generators::YamlGenerator.new(schemas, components: all_components).generate
  puts '✅ OpenAPI specification generated successfully!'
end