Class: RailsOpenapiGen::Importer

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

Instance Method Summary collapse

Constructor Details

#initialize(openapi_file = nil) ⇒ Importer

Initializes importer with OpenAPI specification file

Parameters:

  • openapi_file (String, nil) (defaults to: nil)

    Path to OpenAPI spec file (defaults to configured output)



10
11
12
13
14
15
16
17
# File 'lib/rails-openapi-gen/importer.rb', line 10

def initialize(openapi_file = nil)
  @openapi_file = openapi_file || File.join(
    RailsOpenapiGen.configuration.output_directory,
    RailsOpenapiGen.configuration.output_filename
  )
  @routes_parser = Parsers::RoutesParser.new
  @processed_files = Set.new
end

Instance Method Details

#runvoid

This method returns an undefined value.

Runs the import process to generate @openapi comments in Jbuilder files



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rails-openapi-gen/importer.rb', line 21

def run
  unless File.exist?(@openapi_file)
    puts "❌ OpenAPI file not found: #{@openapi_file}"
    return
  end

  openapi_spec = YAML.load_file(@openapi_file)
  routes = @routes_parser.parse

  processed_count = 0
  @partial_schemas = {} # Store schemas for partials

  # First pass: collect all response schemas
  openapi_spec['paths']&.each do |path, methods|
    methods.each do |method, operation|
      next if method == 'parameters' # Skip path-level parameters

      response_schema = extract_response_schema(operation)
      if response_schema
        if response_schema['type'] == 'array' && response_schema['items']
          # For array responses, store the item schema
          item_schema = response_schema['items']
          if item_schema['properties']
            # Extract resource name from path dynamically
            resource_name = extract_resource_name_from_path(path)
            if resource_name
              @partial_schemas[resource_name] ||= item_schema
            end
            collect_partial_schemas(item_schema['properties'])
          end
        elsif response_schema['properties']
          # For object responses
          collect_partial_schemas(response_schema['properties'])

          # Store the root schema if it's a detail endpoint
          if path.match?(/\{(id|\w+_id)\}$/)
            resource_name = extract_resource_name_from_path(path)
            if resource_name
              @partial_schemas["#{resource_name}_detail"] = response_schema
            end
          end
        end
      end
    end
  end

  # Second pass: process files and add comments
  openapi_spec['paths']&.each do |path, methods|
    methods.each do |method, operation|
      next if method == 'parameters' # Skip path-level parameters

      matching_route = find_matching_route(path, method.upcase, routes)
      next unless matching_route

      controller_info = Parsers::ControllerParser.new(matching_route).parse
      next unless controller_info[:jbuilder_path]

      if add_comments_to_jbuilder(controller_info, operation, matching_route)
        processed_count += 1
      end
    end
  end

  # Third pass: process partial files
  process_partial_files

  puts "✅ Updated #{processed_count} Jbuilder templates with OpenAPI comments"
end