Class: RailsOpenapiGen::Processors::OpenApiSchemaProcessor

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

Overview

High-level processor for generating complete OpenAPI schemas Orchestrates the conversion from AST to full OpenAPI specification

Instance Method Summary collapse

Constructor Details

#initializeOpenApiSchemaProcessor

Returns a new instance of OpenApiSchemaProcessor.



9
10
11
# File 'lib/rails-openapi-gen/processors/openapi_schema_processor.rb', line 9

def initialize
  @ast_processor = AstToSchemaProcessor.new
end

Instance Method Details

#extract_missing_comments(root_node) ⇒ Array<Hash>

Extract missing comment information

Parameters:

  • Root AST node

Returns:

  • Array of missing comment information



121
122
123
124
125
# File 'lib/rails-openapi-gen/processors/openapi_schema_processor.rb', line 121

def extract_missing_comments(root_node)
  missing = []
  extract_missing_comments_recursive(root_node, [], missing)
  missing
end

#generate_component_schema(root_node, schema_name) ⇒ Hash

Generate component schema from AST

Parameters:

  • Root AST node

  • Name for the schema component

Returns:

  • Component schema



89
90
91
92
93
94
95
# File 'lib/rails-openapi-gen/processors/openapi_schema_processor.rb', line 89

def generate_component_schema(root_node, schema_name)
  schema = @ast_processor.process_to_schema(root_node)

  {
    schema_name => schema
  }
end

#generate_operation(root_node, operation_info = nil) ⇒ Hash

Generate complete OpenAPI operation from AST and operation info

Parameters:

  • Root AST node

  • (defaults to: nil)

    Operation-level information

Returns:

  • Complete OpenAPI operation



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
# File 'lib/rails-openapi-gen/processors/openapi_schema_processor.rb', line 47

def generate_operation(root_node, operation_info = nil)
  operation = {}

  # Add operation-level information
  if operation_info
    operation[:operationId] = operation_info[:operation_id] if operation_info[:operation_id]
    operation[:summary] = operation_info[:summary] if operation_info[:summary]
    operation[:description] = operation_info[:description] if operation_info[:description]
    operation[:tags] = operation_info[:tags] if operation_info[:tags]
  end

  # Add responses
  operation[:responses] = generate_response_schema(root_node, operation_info)

  # Add parameters if specified
  if operation_info&.dig(:parameters)
    operation[:parameters] = operation_info[:parameters]
  end

  # Add request body if specified
  if operation_info&.dig(:request_body)
    operation[:requestBody] = operation_info[:request_body]
  end

  operation
end

#generate_path_item(method, root_node, operation_info = nil) ⇒ Hash

Generate path item from AST and operation info

Parameters:

  • HTTP method (get, post, etc.)

  • Root AST node

  • (defaults to: nil)

    Operation-level information

Returns:

  • OpenAPI path item



79
80
81
82
83
# File 'lib/rails-openapi-gen/processors/openapi_schema_processor.rb', line 79

def generate_path_item(method, root_node, operation_info = nil)
  {
    method.downcase => generate_operation(root_node, operation_info)
  }
end

#generate_response_schema(root_node, operation_info = nil) ⇒ Hash

Generate complete OpenAPI response schema from AST

Parameters:

  • Root AST node

  • (defaults to: nil)

    Operation-level information

Returns:

  • Complete OpenAPI response schema



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails-openapi-gen/processors/openapi_schema_processor.rb', line 17

def generate_response_schema(root_node, operation_info = nil)
  return default_response_schema unless root_node

  # Convert AST to schema
  schema = @ast_processor.process_to_schema(root_node)

  # Build response object
  response = {
    description: operation_info&.dig(:response_description) || 'Successful response',
    content: {
      'application/json' => {
        schema: schema
      }
    }
  }

  # Add examples if available
  if operation_info&.dig(:examples)
    response[:content]['application/json'][:examples] = operation_info[:examples]
  end

  {
    '200' => response
  }
end

#validate_schema(schema) ⇒ Array<String>

Validate generated schema

Parameters:

  • OpenAPI schema to validate

Returns:

  • Array of validation errors (empty if valid)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails-openapi-gen/processors/openapi_schema_processor.rb', line 100

def validate_schema(schema)
  errors = []

  # Basic validation
  unless schema.is_a?(Hash)
    errors << "Schema must be a hash"
    return errors
  end

  # Check for required fields in responses
  if schema.dig('200', 'content', 'application/json', 'schema')
    schema_obj = schema['200']['content']['application/json']['schema']
    errors.concat(validate_schema_object(schema_obj, 'root'))
  end

  errors
end