lib/dry/swagger/documentation_generator.rb in dry-swagger-0.4.0 vs lib/dry/swagger/documentation_generator.rb in dry-swagger-0.4.1
- old
+ new
@@ -17,11 +17,11 @@
def generate_documentation(fields)
documentation = { properties: {}, required: [] }
fields.each do |field_name, attributes_hash|
documentation[:properties][field_name] = generate_field_properties(attributes_hash)
- documentation[:required] << field_name if attributes_hash[:required] && @config.enable_required_validation
+ documentation[:required] << field_name if attributes_hash.fetch(:required, true) && @config.enable_required_validation
rescue Errors::MissingTypeError => e
raise StandardError.new e.message % { field_name: field_name, valid_types: SWAGGER_FIELD_TYPE_DEFINITIONS.keys, attributes_hash: attributes_hash }
rescue Errors::MissingHashSchemaError => e
raise StandardError.new e.message % { field_name: field_name, valid_types: SWAGGER_FIELD_TYPE_DEFINITIONS.keys, attributes_hash: attributes_hash }
@@ -30,26 +30,37 @@
{ :type => :object, :properties => documentation[:properties], :required => documentation[:required] }
end
def generate_field_properties(attributes_hash)
if attributes_hash[:type] == 'array'
- items = generate_documentation(attributes_hash[:keys])
- items.merge(@config.nullable_type => attributes_hash[@config.nullable_type] | false) if @config.enable_nullable_validation
- { type: :array, items: items }
- elsif attributes_hash[:array] && attributes_hash[:type] != 'array'
- items = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash[:type])
- items = items.merge(@config.nullable_type => attributes_hash[@config.nullable_type] | false) if @config.enable_nullable_validation
- { type: :array, items: items }
+ items = generate_documentation(attributes_hash.fetch(:keys))
+ items = @config.enable_nullable_validation ?
+ items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
+ items.merge(@config.nullable_type => true)
+ documentation = { type: :array, items: items }
+ elsif attributes_hash[:array] && attributes_hash.fetch(:type) != 'array'
+ items = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
+ items = @config.enable_nullable_validation ?
+ items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
+ items.merge(@config.nullable_type => true)
+ documentation = { type: :array, items: items }
elsif attributes_hash[:type] == 'hash'
raise Errors::MissingHashSchemaError.new unless attributes_hash[:keys]
- generate_documentation(attributes_hash[:keys])
+ documentation = generate_documentation(attributes_hash.fetch(:keys))
else
- field = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash[:type])
- field = field.merge(@config.nullable_type => attributes_hash[@config.nullable_type] | false) if @config.enable_nullable_validation
- field = field.merge(enum: attributes_hash[:enum]) if attributes_hash[:enum] if @config.enable_enums
- field = field.merge(description: attributes_hash[:description]) if attributes_hash[:description] if @config.enable_descriptions
- field
+ documentation = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
+ if attributes_hash[:enum] && @config.enable_enums
+ documentation = documentation.merge(enum: attributes_hash.fetch(:enum))
+ end
+
+ if attributes_hash[:description] && @config.enable_descriptions
+ documentation = documentation.merge(description: attributes_hash.fetch(:description))
+ end
end
+
+ @config.enable_nullable_validation ?
+ documentation.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
+ documentation.merge(@config.nullable_type => true)
rescue KeyError
raise Errors::MissingTypeError.new
end
end