Skip to content

Commit

Permalink
Fix additional JSON schema generation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap committed Sep 11, 2024
1 parent 4222123 commit b7f1952
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion lib/convert_json_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def convert_to_json_schema(props)
props.delete("required") unless props["default"].nil?

# Loop through each field
props.each_with_object({}) do |(k, v), fields|
props = props.each_with_object({}) do |(k, v), fields|
v = convert_type(v) if k == 'type'
k = 'properties' if k == 'fields'
k = 'minimum' if k == 'gt'
Expand All @@ -75,6 +75,7 @@ def convert_to_json_schema(props)
fields['format'] = 'uuid'
end


# Remove unused fields
next if [
'entity_checks',
Expand Down Expand Up @@ -103,13 +104,30 @@ def convert_to_json_schema(props)

fields[k] = v
end

if props['type'] == "number" && props['enum']
allPropsAreIntegers = props['enum'].select { |e| e.is_a?(Integer) }.length == props['enum'].length
if allPropsAreIntegers
props['type'] = 'integer'
end
end

props
end

def convert_required_list(schema)

schema['required'] = [] if !schema['required'].is_a?(Array)

if schema['properties']

# Fix empty schema properties that should
# be additionalProperties = true
if schema['properties'].is_a?(Array)
schema['properties'] = {}
schema['additionalProperties'] = true
end

schema['properties'].each do |k, v|
if v['required']
schema['required'].push(k)
Expand Down Expand Up @@ -148,14 +166,56 @@ def remove_object_required_optional_children(schema)
schema
end

def fix_regex(schema)
if schema['pattern']
# Convert Lua pattern to regex
lua_patterns = {
'%a' => '[a-zA-Z]',
'%c' => '[\x00-\x1F]', # Control characters, cannot be replaced with actual characters
'%d' => '[0-9]',
'%g' => '[\x21-\x7E]', # Printable characters, cannot be replaced with actual characters
'%l' => '[a-z]',
'%p' => '[!-/:-@[-`{-~]', # Punctuation characters
'%s' => '[\t\n\v\f\r ]', # Whitespace characters
'%u' => '[A-Z]',
'%w' => '[a-zA-Z0-9]',
'%x' => '[0-9a-fA-F]'
}

negative_patterns = {}
lua_patterns.each do |lua_pattern, regex|
negative_patterns["[^"+lua_pattern+"]"] = "[^#{regex[1..-2]}]"
end

lua_patterns = negative_patterns.merge(lua_patterns)

lua_patterns.each do |lua_pattern, regex|
schema['pattern'] = schema['pattern'].gsub(lua_pattern, regex)
end

# Escape forward slashes
schema['pattern'] = schema['pattern'].gsub('%/', '\\/')
end
if schema['items']
schema['items'] = fix_regex(schema['items'])
end

return schema
end

def fix_broken_defaults(schema)
if schema['default'] && schema['type'] == 'object' && schema['default'].is_a?(Array)
schema.delete('default')
end

if schema['default'] && schema['type'] == 'array' && schema['default'].is_a?(Hash)
schema['default'] = [schema['default']]
end

if schema['properties']
schema['properties'].each do |k, v|
schema['properties'][k] = fix_broken_defaults(v)
schema['properties'][k] = fix_regex(v)
end
end

Expand Down

0 comments on commit b7f1952

Please sign in to comment.