Module: OasRails::JsonSchemaGenerator

Defined in:
lib/oas_rails/json_schema_generator.rb

Overview

The JsonSchemaGenerator module provides methods to transform string representations of data types into JSON schema formats.

Class Method Summary collapse

Class Method Details

.parse_object_properties(str) ⇒ Hash

Parses the properties of an object type from a string.

Parameters:

  • str (String)

    The string representation of the object’s properties.

Returns:

  • (Hash)

    A hash where keys are property names and values are their JSON schema types.



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
# File 'lib/oas_rails/json_schema_generator.rb', line 41

def self.parse_object_properties(str)
  properties = {}
  stack = []
  current_key = ''
  current_value = ''

  str.each_char.with_index do |char, index|
    case char
    when '{', '<'
      stack.push(char)
      current_value += char
    when '}', '>'
      stack.pop
      current_value += char
    when ','
      if stack.empty?
        properties[current_key.strip.to_sym] = parse_type(current_value.strip)
        current_key = ''
        current_value = ''
      else
        current_value += char
      end
    when ':'
      if stack.empty?
        current_key = current_value
        current_value = ''
      else
        current_value += char
      end
    else
      current_value += char
    end

    properties[current_key.strip.to_sym] = parse_type(current_value.strip) if index == str.length - 1 && !current_key.empty?
  end

  properties
end

.parse_type(str) ⇒ Hash

Parses a string representing a data type and determines its JSON schema type.

Parameters:

  • str (String)

    The string representation of a data type.

Returns:

  • (Hash)

    A hash containing the type, whether it’s required, and any additional properties.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/oas_rails/json_schema_generator.rb', line 23

def self.parse_type(str)
  required = str.start_with?('!')
  type = str.sub(/^!/, '').strip

  case type
  when /^Hash\{(.+)\}$/i
    { type: :object, required:, properties: parse_object_properties(::Regexp.last_match(1)) }
  when /^Array<(.+)>$/i
    { type: :array, required:, items: parse_type(::Regexp.last_match(1)) }
  else
    { type: type.downcase.to_sym, required: }
  end
end

.process_string(str) ⇒ Hash

Processes a string representing a data type and converts it into a JSON schema.

Parameters:

  • str (String)

    The string representation of a data type.

Returns:

  • (Hash)

    A hash containing the required flag and the JSON schema.



11
12
13
14
15
16
17
# File 'lib/oas_rails/json_schema_generator.rb', line 11

def self.process_string(str)
  parsed = parse_type(str)
  {
    required: parsed[:required],
    json_schema: to_json_schema(parsed)
  }
end

.ruby_type_to_json_schema_type(type) ⇒ Hash, String

Converts a Ruby data type into its corresponding JSON schema type.

Parameters:

  • type (Symbol, String)

    The Ruby data type.

Returns:

  • (Hash, String)

    The JSON schema type or a hash with additional format information.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/oas_rails/json_schema_generator.rb', line 112

def self.ruby_type_to_json_schema_type(type)
  case type.to_s.downcase
  when 'string' then { type: "string" }
  when 'integer' then { type: "integer" }
  when 'float' then { type: "float" }
  when 'boolean' then { type: "boolean" }
  when 'array' then { type: "array" }
  when 'hash' then { type: "hash" }
  when 'nil' then { type: "null" }
  when 'date' then { type: "string", format: "date" }
  when 'datetime' then { type: "string", format: "date-time" }
  else type.to_s.downcase
  end
end

.to_json_schema(parsed) ⇒ Hash

Converts a parsed data type into a JSON schema format.

Parameters:

  • parsed (Hash)

    The parsed data type hash.

Returns:

  • (Hash)

    The JSON schema representation of the parsed data type.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/oas_rails/json_schema_generator.rb', line 84

def self.to_json_schema(parsed)
  case parsed[:type]
  when :object
    schema = {
      type: 'object',
      properties: {}
    }
    required_props = []
    parsed[:properties].each do |key, value|
      schema[:properties][key] = to_json_schema(value)
      required_props << key.to_s if value[:required]
    end
    schema[:required] = required_props unless required_props.empty?
    schema
  when :array
    {
      type: 'array',
      items: to_json_schema(parsed[:items])
    }
  else
    ruby_type_to_json_schema_type(parsed[:type])
  end
end