lib/dry/swagger/documentation_generator.rb in dry-swagger-1.0.0 vs lib/dry/swagger/documentation_generator.rb in dry-swagger-2.0.0
- old
+ new
@@ -9,14 +9,22 @@
"datetime" => { type: :string, format: :datetime },
"date" => { type: :string, format: :date },
"time" => { type: :string, format: :time },
}.freeze
- def initialize(config)
- @config = config
+ def initialize
+ @config = Config::SwaggerConfiguration
end
+ def from_struct(struct)
+ generate_documentation(DryStructParser::StructSchemaParser.new.call(struct).keys)
+ end
+
+ def from_validation(validation)
+ generate_documentation(DryValidationParser::ValidationSchemaParser.new.call(validation).keys)
+ end
+
def generate_documentation(fields)
documentation = { properties: {}, required: [] }
fields.each do |field_name, definition|
documentation[:properties][field_name] = generate_field_properties(definition)
if definition.is_a?(Hash)
@@ -43,11 +51,11 @@
documentation = generate_for_hash(definition)
else
documentation = generate_for_primitive_type(definition)
end
@config.enable_nullable_validation ?
- documentation.merge(@config.nullable_type => definition.fetch(@config.nullable_type, false)) :
+ documentation.merge(@config.nullable_type => definition.fetch(:nullable, false)) :
documentation.merge(@config.nullable_type => true)
rescue KeyError
raise Errors::MissingTypeError.new
end
@@ -78,10 +86,10 @@
def generate_for_array(definition)
items = array_of_primitive_type?(definition) ?
SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(definition.fetch(:type)) :
generate_documentation(definition.fetch(:keys))
items = @config.enable_nullable_validation ?
- items.merge(@config.nullable_type => definition.fetch(@config.nullable_type, false)) :
+ items.merge(@config.nullable_type => definition.fetch(:nullable, false)) :
items.merge(@config.nullable_type => true)
{ type: :array, items: items }
end
def generate_for_hash(definition)