jekyll-openapi 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,11 @@
1
+ require_relative "../schema"
2
+
1
3
  module JekyllOpenAPI
2
4
  module SchemaRenderer
3
5
  ##
4
6
  # Renders a JSON-Schema as a simplified XML representation of the object,
5
7
  # honoring the OpenAPI `xml` metadata object (name, prefix, namespace,
6
- # attribute, wrapped).
8
+ # attribute, wrapped). Traversal and `$ref` following live in {Schema}.
7
9
  # @see https://spec.openapis.org/oas/v3.1.0.html#xml-object
8
10
  class XML
9
11
  def initialize(logger: JekyllOpenAPI.logger)
@@ -14,17 +16,27 @@ module JekyllOpenAPI
14
16
  new(logger: logger).render(schema)
15
17
  end
16
18
 
17
- # @param input [Hash] a JSON-Schema
19
+ # @param input [Hash, Reference, Schema] a JSON-Schema
18
20
  # @param prefix [String] current indentation
19
21
  # @param name [String] element name when the schema has no xml.name
20
22
  # @return [String]
21
23
  def render(input, prefix = "", name = "xml")
22
- unless input.is_a?(Hash) && (input["type"] || input["properties"] || input["items"])
23
- @logger.warn("not a valid schema object: #{input.inspect}")
24
+ schema = Schema.wrap(input)
25
+
26
+ if schema.reference?
27
+ followed = schema.follow
28
+ # A cyclic or unresolvable ref has no finite XML expansion here.
29
+ return "" if followed.nil? || followed == Schema::CYCLE
30
+ return render(followed, prefix, name)
31
+ end
32
+
33
+ unless schema.mapping? &&
34
+ (schema.declared_type || schema["properties"] || schema["items"] || schema["additionalProperties"])
35
+ @logger.warn("not a valid schema object: #{schema.raw.inspect}")
24
36
  return ""
25
37
  end
26
38
 
27
- xml_def = input["xml"] || {}
39
+ xml_def = schema.xml
28
40
  name = xml_def["name"] || name
29
41
  name = "#{xml_def["prefix"]}:#{name}" if xml_def["prefix"]
30
42
  attrs = {}
@@ -33,40 +45,44 @@ module JekyllOpenAPI
33
45
  attrs[ns_attr] = xml_def["namespace"]
34
46
  end
35
47
 
36
- type = input["type"]
37
- type ||= "object" if input["properties"]
38
- type ||= "array" if input["items"]
48
+ type = schema.declared_type
49
+ # A 3.1 type union (e.g. ["string","null"]) prints as one sample element:
50
+ # take the first non-null member rather than stringifying the array.
51
+ type = (type - ["null"]).first if type.is_a?(Array)
52
+ type ||= "object" if schema["properties"] || schema["additionalProperties"]
53
+ type ||= "array" if schema["items"]
39
54
 
40
55
  case type
41
56
  when "array"
42
- array(input, prefix, name, attrs, wrapped: xml_def["wrapped"])
57
+ array(schema, prefix, name, attrs, wrapped: xml_def["wrapped"])
43
58
  when "object"
44
59
  content = ""
45
- (input["properties"] || {}).each do |key, schema|
46
- if schema.is_a?(Hash) && schema.dig("xml", "attribute")
47
- attrs[schema.dig("xml", "name") || key] = attribute_value(schema)
60
+ schema.properties.each do |key, property|
61
+ if property.dig("xml", "attribute")
62
+ attrs[property.dig("xml", "name") || key] = attribute_value(property)
48
63
  else
49
- content += "\n#{render(schema, prefix + " ", key)}"
64
+ content += "\n#{render(property, prefix + " ", key)}"
50
65
  end
51
66
  end
52
67
  element(name, attrs, content, prefix)
53
68
  when "string"
54
- element(name, attrs, string_content(input), prefix)
69
+ element(name, attrs, string_content(schema), prefix)
55
70
  else
56
71
  content = type.to_s
57
- content += comment(" (#{input["format"]})") if input["format"]
58
- content += comment(input["summary"]) if input["summary"]
72
+ content += comment(" (#{schema.format})") if schema.format
73
+ content += comment(schema.summary) if schema.summary
59
74
  element(name, attrs, content, prefix)
60
75
  end
61
76
  end
62
77
 
63
78
  private
64
79
 
65
- def array(input, prefix, name, attrs, wrapped: false)
80
+ def array(schema, prefix, name, attrs, wrapped: false)
81
+ items = schema.items
66
82
  unless wrapped
67
- return "#{render(input["items"], prefix, name)}\n#{prefix}#{comment("Array of <#{name}>")}"
83
+ return "#{render(items, prefix, name)}\n#{prefix}#{comment("Array of <#{name}>")}"
68
84
  end
69
- item = render(input["items"], prefix + " ", singular(name))
85
+ item = render(items, prefix + " ", singular(name))
70
86
  content = "\n#{item}\n#{prefix + " "}#{comment("Array of <#{singular(name)}>")}"
71
87
  element(name, attrs, content, prefix)
72
88
  end
@@ -77,20 +93,23 @@ module JekyllOpenAPI
77
93
  "#{prefix}<#{name}#{str_attrs}>#{content}#{closing}</#{name}>"
78
94
  end
79
95
 
80
- def string_content(input)
81
- content = if input["format"] == "binary"
96
+ def string_content(schema)
97
+ content = if schema.format == "binary"
82
98
  "Buffer"
83
- elsif input["example"]
84
- input["example"].to_s
85
- elsif input["examples"].is_a?(Hash) && !input["examples"].empty?
86
- input["examples"].values.first["value"].to_s
99
+ elsif !schema.example.nil?
100
+ schema.example.to_s
101
+ elsif schema.examples.is_a?(Array) && !schema.examples.empty?
102
+ # OpenAPI 3.1 / JSON-Schema: a schema's `examples` is an array of values.
103
+ # The Example Object *map* is a media-type/parameter construct, not valid
104
+ # on a schema, so it is deliberately not interpreted here.
105
+ schema.examples.first.to_s
87
106
  else
88
107
  "string"
89
108
  end
90
- if input["summary"]
91
- content += " #{comment(input["summary"])}"
92
- elsif input["pattern"]
93
- content += comment("/#{input["pattern"]}/")
109
+ if schema.summary
110
+ content += " #{comment(schema.summary)}"
111
+ elsif schema.pattern
112
+ content += comment("/#{schema.pattern}/")
94
113
  end
95
114
  content
96
115
  end
@@ -1,3 +1,3 @@
1
1
  module JekyllOpenAPI
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,10 +1,17 @@
1
1
  require_relative "jekyll_openapi/version"
2
2
  require_relative "jekyll_openapi/json_pointer"
3
+ require_relative "jekyll_openapi/reference"
3
4
  require_relative "jekyll_openapi/dereferencer"
4
5
  require_relative "jekyll_openapi/parameters"
6
+ require_relative "jekyll_openapi/examples"
7
+ require_relative "jekyll_openapi/anchors"
8
+ require_relative "jekyll_openapi/content_type"
9
+ require_relative "jekyll_openapi/headers"
10
+ require_relative "jekyll_openapi/schema"
5
11
  require_relative "jekyll_openapi/document"
6
12
  require_relative "jekyll_openapi/schema_renderer/typescript"
7
13
  require_relative "jekyll_openapi/schema_renderer/xml"
14
+ require_relative "jekyll_openapi/schema_renderer/html"
8
15
  require_relative "jekyll_openapi/filters"
9
16
 
10
17
  module JekyllOpenAPI
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Dumetz
@@ -67,12 +67,19 @@ files:
67
67
  - README.md
68
68
  - lib/jekyll-openapi.rb
69
69
  - lib/jekyll_openapi.rb
70
+ - lib/jekyll_openapi/anchors.rb
71
+ - lib/jekyll_openapi/content_type.rb
70
72
  - lib/jekyll_openapi/dereferencer.rb
71
73
  - lib/jekyll_openapi/document.rb
74
+ - lib/jekyll_openapi/examples.rb
72
75
  - lib/jekyll_openapi/filters.rb
76
+ - lib/jekyll_openapi/headers.rb
73
77
  - lib/jekyll_openapi/jekyll_plugin.rb
74
78
  - lib/jekyll_openapi/json_pointer.rb
75
79
  - lib/jekyll_openapi/parameters.rb
80
+ - lib/jekyll_openapi/reference.rb
81
+ - lib/jekyll_openapi/schema.rb
82
+ - lib/jekyll_openapi/schema_renderer/html.rb
76
83
  - lib/jekyll_openapi/schema_renderer/typescript.rb
77
84
  - lib/jekyll_openapi/schema_renderer/xml.rb
78
85
  - lib/jekyll_openapi/version.rb