Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplification for reporting an 'any' type in OpenApi #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Attributor Changelog

## next
- added support for enum's out of values in json_schema generation
- Correctly report an Object type as a schema without a :type in open api output

## 6.1 (1/7/2022)
- added support for enum's out of values in json_schema generation
Expand Down
12 changes: 8 additions & 4 deletions lib/attributor/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def json_schema_string_format

def as_json_schema( shallow: false, example: nil, attribute_options: {} )
type_name = self.ancestors.find { |k| k.name && !k.name.empty? }.name
hash = { type: json_schema_type, 'x-type_name': type_name.gsub( Attributor::MODULE_PREFIX_REGEX, '' )}
# Add a format, if the type has defined
if hash[:type] == :string && the_format = json_schema_string_format
hash[:format] = the_format
hash = { 'x-type_name': type_name.gsub( Attributor::MODULE_PREFIX_REGEX, '' )}
if json_schema_type
# A type key does not exist for 'any' (i.e., a nil value for json_schema_type)
hash[:type] = json_schema_type
# Add a format, if the type has defined
if hash[:type] == :string && the_format = json_schema_string_format
hash[:format] = the_format
end
end
# Common options
hash[:enum] = attribute_options[:values] if attribute_options[:values]
Expand Down
13 changes: 3 additions & 10 deletions lib/attributor/types/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@ def self.example(_context = nil, options: {})
'An Object'
end

# Not really used (we override as_json_schema to represent this as an Any Type),
# but if it _were_ used, this would be accurate.
def self.json_schema_type
:object
end

# Represents Object as an OpenAPI Any Type.
# Which means there is no type key for an Object (i.e., 'any'), so we'll report it as nil
#
# @see https://swagger.io/docs/specification/data-models/data-types/#any
def self.as_json_schema(**kwargs)
schema = super(**kwargs)
schema.delete(:type)
schema
def self.json_schema_type
nil
end
end
end