grape_openapi3 0.1.6 → 0.1.7
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +140 -0
- data/LICENSE +21 -0
- data/README.md +175 -12
- data/lib/generators/grape_openapi3/templates/openapi.rake +10 -3
- data/lib/grape_openapi3/builders/operation_builder.rb +40 -13
- data/lib/grape_openapi3/builders/operation_id_builder.rb +7 -4
- data/lib/grape_openapi3/builders/parameter_builder.rb +67 -14
- data/lib/grape_openapi3/config.rb +1 -1
- data/lib/grape_openapi3/diagnostics.rb +46 -0
- data/lib/grape_openapi3/document.rb +34 -2
- data/lib/grape_openapi3/keyword_mapper.rb +63 -0
- data/lib/grape_openapi3/readers/entity_reader.rb +123 -37
- data/lib/grape_openapi3/readers/route_reader.rb +41 -0
- data/lib/grape_openapi3/type_mapper.rb +56 -15
- data/lib/grape_openapi3/values_mapper.rb +70 -0
- data/lib/grape_openapi3/version.rb +1 -1
- data/lib/grape_openapi3.rb +14 -3
- metadata +7 -2
|
@@ -8,6 +8,7 @@ module GrapeOpenapi3
|
|
|
8
8
|
|
|
9
9
|
PRIMITIVES = {
|
|
10
10
|
"String" => { "type" => "string" },
|
|
11
|
+
"Symbol" => { "type" => "string" },
|
|
11
12
|
"Integer" => { "type" => "integer" },
|
|
12
13
|
"Float" => { "type" => "number", "format" => "float" },
|
|
13
14
|
"BigDecimal" => { "type" => "number", "format" => "double" },
|
|
@@ -20,22 +21,50 @@ module GrapeOpenapi3
|
|
|
20
21
|
"File" => { "type" => "string", "format" => "binary" },
|
|
21
22
|
"Hash" => { "type" => "object" },
|
|
22
23
|
"Array" => { "type" => "array", "items" => {} },
|
|
24
|
+
# OpenAPI type names, as often written in grape-swagger style docs
|
|
25
|
+
"string" => { "type" => "string" },
|
|
26
|
+
"integer" => { "type" => "integer" },
|
|
27
|
+
"number" => { "type" => "number" },
|
|
28
|
+
"object" => { "type" => "object" },
|
|
29
|
+
"array" => { "type" => "array", "items" => {} },
|
|
23
30
|
}.freeze
|
|
24
31
|
|
|
25
32
|
def self.call(type)
|
|
26
33
|
new(type).call
|
|
27
34
|
end
|
|
28
35
|
|
|
36
|
+
# The name of the first type the mapper does not know (and would fall back
|
|
37
|
+
# to string for), or nil when every type is known. Used by strict mode.
|
|
38
|
+
def self.unknown_type(type)
|
|
39
|
+
new(type).unknown_type
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def unknown_type
|
|
43
|
+
return nil if boolean?
|
|
44
|
+
return @type.filter_map { |t| TypeMapper.unknown_type(t) }.first if @type.is_a?(Array)
|
|
45
|
+
return string_list.filter_map { |t| TypeMapper.unknown_type(t) }.first if string_list
|
|
46
|
+
|
|
47
|
+
name = type_name(@type)
|
|
48
|
+
name == "JSON" || PRIMITIVES.key?(name) ? nil : name
|
|
49
|
+
end
|
|
50
|
+
|
|
29
51
|
def initialize(type)
|
|
30
52
|
@type = type
|
|
31
53
|
end
|
|
32
54
|
|
|
55
|
+
# Grape's JSON type accepts a Hash or an Array of Hashes.
|
|
56
|
+
JSON_SCHEMA = {
|
|
57
|
+
"oneOf" => [{ "type" => "object" }, { "type" => "array", "items" => { "type" => "object" } }],
|
|
58
|
+
}.freeze
|
|
59
|
+
|
|
33
60
|
def call
|
|
34
61
|
return { "type" => "boolean" } if boolean?
|
|
35
|
-
return
|
|
36
|
-
return
|
|
62
|
+
return list_schema(@type) if @type.is_a?(Array)
|
|
63
|
+
return list_schema(string_list) if string_list
|
|
64
|
+
|
|
65
|
+
name = type_name(@type)
|
|
66
|
+
return deep_dup(JSON_SCHEMA) if name == "JSON"
|
|
37
67
|
|
|
38
|
-
name = @type.is_a?(Class) ? @type.name.to_s : @type.to_s
|
|
39
68
|
(PRIMITIVES[name] || { "type" => "string" }).dup
|
|
40
69
|
end
|
|
41
70
|
|
|
@@ -44,25 +73,37 @@ module GrapeOpenapi3
|
|
|
44
73
|
def boolean?
|
|
45
74
|
return true if @type == :boolean || @type == "boolean"
|
|
46
75
|
|
|
47
|
-
|
|
48
|
-
BOOLEAN_CLASS_NAMES.include?(name)
|
|
76
|
+
BOOLEAN_CLASS_NAMES.include?(type_name(@type))
|
|
49
77
|
end
|
|
50
78
|
|
|
51
|
-
def
|
|
52
|
-
|
|
79
|
+
def type_name(type)
|
|
80
|
+
type.is_a?(Module) ? type.name.to_s : type.to_s
|
|
53
81
|
end
|
|
54
82
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
83
|
+
# Grape serializes list types to strings in route.params:
|
|
84
|
+
# "[String]" → ["String"], "[Integer, String]" → ["Integer", "String"]
|
|
85
|
+
def string_list
|
|
86
|
+
return @string_list if defined?(@string_list)
|
|
87
|
+
|
|
88
|
+
match = @type.is_a?(String) && @type.match(/\A\[(.*)\]\z/)
|
|
89
|
+
@string_list = match && match[1].split(",").map(&:strip).reject(&:empty?)
|
|
60
90
|
end
|
|
61
91
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
92
|
+
# [A] → array of A (Array[JSON] → array of objects)
|
|
93
|
+
# [A, B, …] → oneOf, Grape's variant ("multiple types") declaration
|
|
94
|
+
def list_schema(types)
|
|
95
|
+
return { "oneOf" => types.map { |t| TypeMapper.call(t) } } if types.size > 1
|
|
96
|
+
|
|
97
|
+
inner = types.first
|
|
98
|
+
items = if inner.nil? then {}
|
|
99
|
+
elsif type_name(inner) == "JSON" then { "type" => "object" }
|
|
100
|
+
else TypeMapper.call(inner)
|
|
101
|
+
end
|
|
65
102
|
{ "type" => "array", "items" => items }
|
|
66
103
|
end
|
|
104
|
+
|
|
105
|
+
def deep_dup(schema)
|
|
106
|
+
Marshal.load(Marshal.dump(schema))
|
|
107
|
+
end
|
|
67
108
|
end
|
|
68
109
|
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GrapeOpenapi3
|
|
4
|
+
# Maps a Grape `values:` option (params or entity documentation) to OpenAPI
|
|
5
|
+
# schema keywords.
|
|
6
|
+
#
|
|
7
|
+
# 1..100 → { "minimum" => 1, "maximum" => 100 }
|
|
8
|
+
# (0.5..) / (..30) → { "minimum" => 0.5 } / { "maximum" => 30 }
|
|
9
|
+
# %w[a b] → { "enum" => ["a", "b"] }
|
|
10
|
+
# -> { %w[a b] } → { "enum" => ["a", "b"] } (lazy list, evaluated)
|
|
11
|
+
# ->(v) { v.size < 3 } → {} (validator, not a list)
|
|
12
|
+
# { value: 1..5, message: "" } → { "minimum" => 1, "maximum" => 5 }
|
|
13
|
+
class ValuesMapper
|
|
14
|
+
def self.call(values)
|
|
15
|
+
new(values).call
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# BigDecimal and Rational serialize as JSON *strings* ("0.0"); OpenAPI
|
|
19
|
+
# keywords need numbers. Whole values become Integer, the rest Float.
|
|
20
|
+
def self.json_number(value)
|
|
21
|
+
return value if !value.is_a?(Numeric) || value.is_a?(Integer) || value.is_a?(Float)
|
|
22
|
+
|
|
23
|
+
value == value.to_i ? value.to_i : value.to_f
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(values)
|
|
27
|
+
@values = values
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def call
|
|
31
|
+
case @values
|
|
32
|
+
when nil then {}
|
|
33
|
+
when Range then range_keywords
|
|
34
|
+
when Proc then proc_keywords
|
|
35
|
+
when Hash then self.class.call(@values[:value] || @values["value"])
|
|
36
|
+
else { "enum" => Array(@values).map { |v| self.class.json_number(v) } }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def range_keywords
|
|
43
|
+
first = @values.begin
|
|
44
|
+
last = @values.end
|
|
45
|
+
|
|
46
|
+
# Non-numeric ranges ("a".."c") have no min/max — list them when finite.
|
|
47
|
+
unless [first, last].compact.all?(Numeric)
|
|
48
|
+
return first && last ? { "enum" => @values.to_a } : {}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
keywords = {}
|
|
52
|
+
keywords["minimum"] = self.class.json_number(first) unless first.nil?
|
|
53
|
+
unless last.nil?
|
|
54
|
+
keywords["maximum"] = self.class.json_number(last)
|
|
55
|
+
keywords["exclusiveMaximum"] = true if @values.exclude_end?
|
|
56
|
+
end
|
|
57
|
+
keywords
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Only a zero-arity proc returns the list of allowed values; a proc taking
|
|
61
|
+
# an argument is a validator and cannot be expressed in the schema.
|
|
62
|
+
def proc_keywords
|
|
63
|
+
return {} unless @values.arity.zero?
|
|
64
|
+
|
|
65
|
+
self.class.call(@values.call)
|
|
66
|
+
rescue StandardError
|
|
67
|
+
{}
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
data/lib/grape_openapi3.rb
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "grape_openapi3/version"
|
|
4
|
+
|
|
5
|
+
module GrapeOpenapi3
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
end
|
|
8
|
+
|
|
4
9
|
require_relative "grape_openapi3/config"
|
|
10
|
+
require_relative "grape_openapi3/diagnostics"
|
|
5
11
|
require_relative "grape_openapi3/railtie" if defined?(Rails)
|
|
6
12
|
require_relative "grape_openapi3/type_mapper"
|
|
13
|
+
require_relative "grape_openapi3/values_mapper"
|
|
14
|
+
require_relative "grape_openapi3/keyword_mapper"
|
|
7
15
|
require_relative "grape_openapi3/path_normalizer"
|
|
8
16
|
require_relative "grape_openapi3/readers/route_reader"
|
|
9
17
|
require_relative "grape_openapi3/readers/entity_reader"
|
|
@@ -13,8 +21,6 @@ require_relative "grape_openapi3/builders/operation_builder"
|
|
|
13
21
|
require_relative "grape_openapi3/document"
|
|
14
22
|
|
|
15
23
|
module GrapeOpenapi3
|
|
16
|
-
class Error < StandardError; end
|
|
17
|
-
|
|
18
24
|
class << self
|
|
19
25
|
# Generate an OpenAPI 3.0 document from a Grape API class.
|
|
20
26
|
#
|
|
@@ -27,7 +33,11 @@ module GrapeOpenapi3
|
|
|
27
33
|
# @param security_schemes [Hash] e.g. { "Bearer" => { type: "http", scheme: "bearer", bearerFormat: "JWT" } }
|
|
28
34
|
# @param security [Array<Hash>] global security: [{ "Bearer" => [] }]
|
|
29
35
|
# @param tags [Array<Hash>] manual tag list: [{ name: "lessons", description: "..." }]
|
|
36
|
+
# @param strict [Boolean, Symbol] report documentation gaps (untyped exposures/params,
|
|
37
|
+
# unknown types, unresolvable entities, 200 without schema):
|
|
38
|
+
# false (default) ignores them, :warn prints them, true raises StrictModeError
|
|
30
39
|
# @return [Hash] OpenAPI 3.0 document (string keys)
|
|
40
|
+
# @raise [StrictModeError] with strict: true, when there are gaps
|
|
31
41
|
#
|
|
32
42
|
# @example Minimal
|
|
33
43
|
# GrapeOpenapi3.generate(V2::ApiGrape,
|
|
@@ -45,7 +55,7 @@ module GrapeOpenapi3
|
|
|
45
55
|
# security: [{ "Bearer" => [] }]
|
|
46
56
|
# )
|
|
47
57
|
def generate(app, info:, servers: [], security_schemes: {}, security: [], tags: [],
|
|
48
|
-
schema_name_separator: "_")
|
|
58
|
+
schema_name_separator: "_", strict: false)
|
|
49
59
|
config = Config.new(
|
|
50
60
|
info: info,
|
|
51
61
|
servers: servers,
|
|
@@ -53,6 +63,7 @@ module GrapeOpenapi3
|
|
|
53
63
|
security: security,
|
|
54
64
|
tags: tags,
|
|
55
65
|
schema_name_separator: schema_name_separator,
|
|
66
|
+
strict: strict,
|
|
56
67
|
)
|
|
57
68
|
Document.build(app, config)
|
|
58
69
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: grape_openapi3
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- rodrigonbarreto
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: grape
|
|
@@ -89,6 +89,8 @@ executables: []
|
|
|
89
89
|
extensions: []
|
|
90
90
|
extra_rdoc_files: []
|
|
91
91
|
files:
|
|
92
|
+
- CHANGELOG.md
|
|
93
|
+
- LICENSE
|
|
92
94
|
- README.md
|
|
93
95
|
- lib/generators/grape_openapi3/install_generator.rb
|
|
94
96
|
- lib/generators/grape_openapi3/templates/openapi.rake
|
|
@@ -97,12 +99,15 @@ files:
|
|
|
97
99
|
- lib/grape_openapi3/builders/operation_id_builder.rb
|
|
98
100
|
- lib/grape_openapi3/builders/parameter_builder.rb
|
|
99
101
|
- lib/grape_openapi3/config.rb
|
|
102
|
+
- lib/grape_openapi3/diagnostics.rb
|
|
100
103
|
- lib/grape_openapi3/document.rb
|
|
104
|
+
- lib/grape_openapi3/keyword_mapper.rb
|
|
101
105
|
- lib/grape_openapi3/path_normalizer.rb
|
|
102
106
|
- lib/grape_openapi3/railtie.rb
|
|
103
107
|
- lib/grape_openapi3/readers/entity_reader.rb
|
|
104
108
|
- lib/grape_openapi3/readers/route_reader.rb
|
|
105
109
|
- lib/grape_openapi3/type_mapper.rb
|
|
110
|
+
- lib/grape_openapi3/values_mapper.rb
|
|
106
111
|
- lib/grape_openapi3/version.rb
|
|
107
112
|
homepage: https://github.com/rodrigonbarreto/grape_openapi3
|
|
108
113
|
licenses:
|