Class: Aspera::Schema::Documentation
- Inherits:
-
Object
- Object
- Aspera::Schema::Documentation
- Defined in:
- lib/aspera/schema/documentation.rb
Overview
Generate documentation from a JSON Schema: in terminal and in manual.
Information from x- extensions is provided by the caller (annotate).
Constant Summary collapse
- JSON_TYPE_TO_DOC =
Map JSON Schema type names to user-friendly display names
{ 'string' => 'String', 'integer' => 'Integer', 'number' => 'Number', 'boolean' => 'Bool', 'array' => 'Array', 'object' => 'Hash' }.freeze
- COLUMNS =
%w[name type description].freeze
Instance Attribute Summary collapse
- #columns ⇒ Array<String> readonly
Instance Method Summary collapse
-
#build(schema = nil) ⇒ Documentation
Generate a documentation table from a JSON schema.
-
#initialize(formatter, schema, code_highlight: false, annotate: nil, extra_columns: {}) ⇒ Documentation
constructor
A new instance of Documentation.
- #rows ⇒ Object
-
#table ⇒ Array<Array<String>>
First row is the titles (for Markdown table generation).
Constructor Details
#initialize(formatter, schema, code_highlight: false, annotate: nil, extra_columns: {}) ⇒ Documentation
Returns a new instance of Documentation.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/aspera/schema/documentation.rb', line 28 def initialize(formatter, schema, code_highlight: false, annotate: nil, extra_columns: {}) @formatter = formatter @schema = schema @code_highlight = code_highlight @annotate = annotate @extra_columns = extra_columns @columns = COLUMNS.dup.insert(-2, *extra_columns.keys).freeze # Sections: each entry is {header: row_or_nil, rows: []} # A flat schema produces a single section with no header. @sections = [{header: nil, rows: []}] end |
Instance Attribute Details
#columns ⇒ Array<String> (readonly)
48 49 50 |
# File 'lib/aspera/schema/documentation.rb', line 48 def columns @columns end |
Instance Method Details
#build(schema = nil) ⇒ Documentation
Generate a documentation table from a JSON schema
Recursively processes a JSON schema to create a formatted table for manual documentation. Handles nested objects, arrays, and extracts metadata (descriptions, types, enums, deprecations).
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/aspera/schema/documentation.rb', line 63 def build(schema = nil) code = @code_highlight ? ->(c) { "`#{c}`" } : ->(c) { c } schema ||= @schema on_variant = ->(variant_reader, discriminant_property, discriminant_value) do title = variant_reader.current['title'] || variant_reader.current['description'] header = if discriminant_property && discriminant_value desc = @formatter.markdown_text("`#{discriminant_value}`") desc += ": #{@formatter.markdown_text(title)}" if title { 'name' => @formatter.markdown_text("**#{discriminant_property}**"), 'type' => code.call(JSON_TYPE_TO_DOC['string']), 'description' => desc } elsif title {'name' => @formatter.markdown_text("**#{title}**")} end @sections.push({header: header, rows: []}) end schema.each_property(on_variant: on_variant) do |property_schema, _name, property_full_name, required| node = property_schema.current annotations = @annotate ? @annotate.call(node, @formatter) : [] next if annotations.nil? item = {'name' => code.call(property_full_name)} item_type = type_text(node) item_type = "Enum[#{item_type}]" if node.key?('enum') && !item_type.nil? item['type'] = code.call(item_type) unless item_type.nil? @extra_columns.each { |column, value| item[column] = value.call(node, @formatter) } description = [] description.push("DEPRECATED: #{node['x-deprecation']}") if node.key?('x-deprecation') # Render Markdown formatting and split lines description.concat(@formatter.markdown_text(node['description']).split("\n")) if node.key?('description') description.push('Required.') if required description.push("Allowed values: #{node['enum'].map { |v| value_text(v) }.join(', ')}.") if node.key?('enum') description.push("Default: #{value_text(node['default'])}.") if node.key?('default') # OpenAPI 3.1: `examples` array (3.0: `example`) example = node.key?('examples') ? Array(node['examples']).first : node['example'] description.push("Example: #{value_text(example)}.") unless example.nil? description.concat(annotations) item['description'] = description unless description.empty? @sections.last[:rows].push(item) end self end |
#rows ⇒ Object
40 41 42 43 44 45 |
# File 'lib/aspera/schema/documentation.rb', line 40 def rows @sections.flat_map do |section| sorted = section[:rows].sort_by { |i| i['name'].delete('`') } section[:header] ? [section[:header]] + sorted : sorted end end |
#table ⇒ Array<Array<String>>
First row is the titles (for Markdown table generation)
52 53 54 |
# File 'lib/aspera/schema/documentation.rb', line 52 def table [@columns] + rows.map { |row| @columns.map { |field| row[field] } } end |