diff --git a/CHANGELOG.md b/CHANGELOG.md index d20aa3c..4831b1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/attributor/type.rb b/lib/attributor/type.rb index cfbf3ee..0acfd9b 100644 --- a/lib/attributor/type.rb +++ b/lib/attributor/type.rb @@ -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] diff --git a/lib/attributor/types/object.rb b/lib/attributor/types/object.rb index 5fdea6c..0d2d379 100644 --- a/lib/attributor/types/object.rb +++ b/lib/attributor/types/object.rb @@ -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