Class: Aspera::Schema::Documentation

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(formatter, schema, code_highlight: false, annotate: nil, extra_columns: {}) ⇒ Documentation

Returns a new instance of Documentation.

Parameters:

  • formatter (FormatterInterface) —

    Formatter with methods: markdown_text, special_format

  • schema (Reader)
  • code_highlight (Boolean) (defaults to: false) —

    true: format name and type as code

  • annotate (Proc, nil) (defaults to: nil) —

    Called with the schema of a property (Hash) and the formatter. Returns lines added to the description (Array<String>), or nil to hide the property.

  • extra_columns (Hash{String => Proc}) (defaults to: {}) —

    Columns inserted before the description: name => Proc called with the schema of a property (Hash) and the formatter, returns the value of the cell.



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)

Returns:



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).

Parameters:

  • schema (Reader) (defaults to: nil) —

    The JSON schema to process

Returns:



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)

Returns:



52
53
54
# File 'lib/aspera/schema/documentation.rb', line 52

def table
  [@columns] + rows.map { |row| @columns.map { |field| row[field] } }
end