Module: JekyllOpenAPI::Examples

Defined in:
lib/jekyll_openapi/examples.rb

Overview

Flattens the several places and shapes OpenAPI examples take into one list, so a template never has to care where an example came from.

Sources, in priority order, for a Parameter or Media Type object:

1. +examples+        — a map of named Example Objects ({summary, description, value})
2. +example+         — a single, unnamed value
3. +schema.examples+ — JSON-Schema examples: an array of values (3.1), or
                     (tolerated) a named map like (1)
4. +schema.example+  — a single, unnamed value

The first non-empty source wins — this mirrors the spec's "example XOR examples" rule while still reaching the schema-level fallback, so callers get the same result whether examples live on the parameter (3.x style) or on its schema. Each entry is a plain, string-keyed Hash so it is Liquid-friendly: "value", "summary", "description" (name is nil for unnamed values).

Class Method Summary collapse

Class Method Details

.normalize(object) ⇒ Array<Hash>

Returns normalized examples ([] when there are none).

Parameters:

  • object (Hash, Reference)

    a Parameter or Media Type object

Returns:

  • (Array<Hash>)

    normalized examples ([] when there are none)



23
24
25
26
27
28
29
30
31
# File 'lib/jekyll_openapi/examples.rb', line 23

def self.normalize(object)
  return [] unless mapping?(object)

  from_map_or_array(object["examples"]) ||
    from_value(object["example"]) ||
    from_map_or_array(dig(object, "schema", "examples")) ||
    from_value(dig(object, "schema", "example")) ||
    []
end