Class: Dry::Swagger::DocumentationGenerator
- Inherits:
-
Object
- Object
- Dry::Swagger::DocumentationGenerator
- Defined in:
- lib/dry/swagger/documentation_generator.rb
Constant Summary collapse
- SWAGGER_FIELD_TYPE_DEFINITIONS =
{ "string" => { type: :string }, "integer" => { type: :integer }, "boolean" => { type: :boolean }, "float" => { type: :float }, "decimal" => { type: :string, format: :decimal }, "datetime" => { type: :string, format: :datetime }, "date" => { type: :string, format: :date }, "time" => { type: :string, format: :time }, "uuid" => { type: :string, format: :uuid } }.freeze
Instance Method Summary collapse
- #array_of_primitive_type?(definition) ⇒ Boolean
- #from_struct(struct) ⇒ Object
- #from_validation(validation) ⇒ Object
- #generate_documentation(fields) ⇒ Object
- #generate_field_properties(definition) ⇒ Object
- #generate_for_array(definition) ⇒ Object
- #generate_for_hash(definition) ⇒ Object
- #generate_for_primitive_type(definition) ⇒ Object
- #generate_for_sti_type(definition) ⇒ Object
-
#initialize ⇒ DocumentationGenerator
constructor
A new instance of DocumentationGenerator.
Constructor Details
#initialize ⇒ DocumentationGenerator
Returns a new instance of DocumentationGenerator.
16 17 18 |
# File 'lib/dry/swagger/documentation_generator.rb', line 16 def initialize @config = ::Dry::Swagger::Config::SwaggerConfiguration end |
Instance Method Details
#array_of_primitive_type?(definition) ⇒ Boolean
113 114 115 |
# File 'lib/dry/swagger/documentation_generator.rb', line 113 def array_of_primitive_type?(definition) definition[:array] && definition.fetch(:type) != 'array' end |
#from_struct(struct) ⇒ Object
20 21 22 |
# File 'lib/dry/swagger/documentation_generator.rb', line 20 def from_struct(struct) generate_documentation(::DryStructParser::StructSchemaParser.new.call(struct).keys) end |
#from_validation(validation) ⇒ Object
24 25 26 |
# File 'lib/dry/swagger/documentation_generator.rb', line 24 def from_validation(validation) generate_documentation(::DryValidationParser::ValidationSchemaParser.new.call(validation).keys) end |
#generate_documentation(fields) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dry/swagger/documentation_generator.rb', line 28 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) documentation[:required] << field_name if definition.fetch(:required, true) && @config.enable_required_validation else documentation[:required] << field_name if definition[0].fetch(:required, true) && @config.enable_required_validation end rescue Errors::MissingTypeError => e raise StandardError.new e. % { field_name: field_name, valid_types: SWAGGER_FIELD_TYPE_DEFINITIONS.keys, definition: definition } rescue Errors::MissingHashSchemaError => e raise StandardError.new e. % { field_name: field_name, valid_types: SWAGGER_FIELD_TYPE_DEFINITIONS.keys, definition: definition } end result = { :type => :object, :properties => documentation[:properties] } result[:required] = documentation[:required] if documentation[:required].any? result end |
#generate_field_properties(definition) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dry/swagger/documentation_generator.rb', line 49 def generate_field_properties(definition) return generate_for_sti_type(definition) if definition.is_a?(Array) if definition[:type] == 'array' || definition[:array] documentation = generate_for_array(definition) elsif definition[:type] == 'hash' documentation = generate_for_hash(definition) else documentation = generate_for_primitive_type(definition) end @config.enable_nullable_validation ? documentation.merge(@config.nullable_type => definition.fetch(:nullable, false)) : documentation.merge(@config.nullable_type => true) rescue KeyError raise Errors::MissingTypeError.new end |
#generate_for_array(definition) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/dry/swagger/documentation_generator.rb', line 90 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(:nullable, false)) : items.merge(@config.nullable_type => true) { type: :array, items: items } end |
#generate_for_hash(definition) ⇒ Object
100 101 102 103 |
# File 'lib/dry/swagger/documentation_generator.rb', line 100 def generate_for_hash(definition) raise Errors::MissingHashSchemaError.new unless definition[:keys] generate_documentation(definition.fetch(:keys)) end |
#generate_for_primitive_type(definition) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/dry/swagger/documentation_generator.rb', line 105 def generate_for_primitive_type(definition) documentation = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(definition.fetch(:type)) documentation = documentation.merge(enum: definition.fetch(:enum)) if definition[:enum] && @config.enable_enums documentation = documentation.merge(description: definition.fetch(:description)) if definition[:description] && @config.enable_descriptions documentation end |
#generate_for_sti_type(definition) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/dry/swagger/documentation_generator.rb', line 67 def generate_for_sti_type(definition) properties = {} definition.each_with_index do |_, index| properties["definition_#{index + 1}"] = generate_field_properties(definition[index]) end documentation = { type: :object, properties: properties, example: 'Dynamic Field. See Model Definitions' } if definition[0][:type] == 'array' definition.each { |it| it[:type] = 'hash'} documentation[:oneOf] = definition.map{ |it| generate_field_properties(it) } { type: :array, items: documentation } else documentation[:oneOf] = definition.map{ |it| generate_field_properties(it) } documentation end end |