Class: JekyllOpenAPI::SchemaRenderer::XML

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_openapi/schema_renderer/xml.rb

Overview

Renders a JSON-Schema as a simplified XML representation of the object, honoring the OpenAPI xml metadata object (name, prefix, namespace, attribute, wrapped).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: JekyllOpenAPI.logger) ⇒ XML

Returns a new instance of XML.



9
10
11
# File 'lib/jekyll_openapi/schema_renderer/xml.rb', line 9

def initialize(logger: JekyllOpenAPI.logger)
  @logger = logger
end

Class Method Details

.render(schema, logger: JekyllOpenAPI.logger) ⇒ Object



13
14
15
# File 'lib/jekyll_openapi/schema_renderer/xml.rb', line 13

def self.render(schema, logger: JekyllOpenAPI.logger)
  new(logger: logger).render(schema)
end

Instance Method Details

#render(input, prefix = "", name = "xml") ⇒ String

Parameters:

  • input (Hash)

    a JSON-Schema

  • prefix (String) (defaults to: "")

    current indentation

  • name (String) (defaults to: "xml")

    element name when the schema has no xml.name

Returns:

  • (String)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jekyll_openapi/schema_renderer/xml.rb', line 21

def render(input, prefix = "", name = "xml")
  unless input.is_a?(Hash) && (input["type"] || input["properties"] || input["items"])
    @logger.warn("not a valid schema object: #{input.inspect}")
    return ""
  end

  xml_def = input["xml"] || {}
  name = xml_def["name"] || name
  name = "#{xml_def["prefix"]}:#{name}" if xml_def["prefix"]
  attrs = {}
  if xml_def["namespace"]
    ns_attr = xml_def["prefix"] ? "xmlns:#{xml_def["prefix"]}" : "xmlns"
    attrs[ns_attr] = xml_def["namespace"]
  end

  type = input["type"]
  type ||= "object" if input["properties"]
  type ||= "array" if input["items"]

  case type
  when "array"
    array(input, prefix, name, attrs, wrapped: xml_def["wrapped"])
  when "object"
    content = ""
    (input["properties"] || {}).each do |key, schema|
      if schema.is_a?(Hash) && schema.dig("xml", "attribute")
        attrs[schema.dig("xml", "name") || key] = attribute_value(schema)
      else
        content += "\n#{render(schema, prefix + "  ", key)}"
      end
    end
    element(name, attrs, content, prefix)
  when "string"
    element(name, attrs, string_content(input), prefix)
  else
    content = type.to_s
    content += comment(" (#{input["format"]})") if input["format"]
    content += comment(input["summary"]) if input["summary"]
    element(name, attrs, content, prefix)
  end
end