Module: OasCore::JsonSchemaGenerator

Defined in:
lib/oas_core/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

rubocop:disable Metrics 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.



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
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/oas_core/json_schema_generator.rb', line 60

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

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

  properties
end

.parse_type(str) ⇒ Hash

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

Registry for custom type parsers

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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oas_core/json_schema_generator.rb', line 36

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
    custom_parser = @custom_type_parsers.find { |matcher, _| matcher.call(type) }
    if custom_parser
      custom_parser.last.call(type, required)
    else
      { type: type.downcase.to_sym, required: }
    end
  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.



23
24
25
26
27
28
29
# File 'lib/oas_core/json_schema_generator.rb', line 23

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

.register_type_parser(type_matcher, parser) ⇒ Object

Registers a custom type parser.

Parameters:

  • type_matcher (Proc)

    A proc that matches the type string.

  • parser (Proc)

    A proc that processes the type string.



15
16
17
# File 'lib/oas_core/json_schema_generator.rb', line 15

def self.register_type_parser(type_matcher, parser)
  @custom_type_parsers[type_matcher] = parser
end

.ruby_type_to_json_schema_type(type) ⇒ Hash, String

rubocop:disable Metrics 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.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/oas_core/json_schema_generator.rb', line 138

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' }
  when 'file' then { type: 'string', format: 'binary' }
  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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/oas_core/json_schema_generator.rb', line 107

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])
    }
  when nil
    parsed
  else
    ruby_type_to_json_schema_type(parsed[:type])
  end
end