sober_swag 0.18.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +15 -0
  3. data/.github/workflows/benchmark.yml +39 -0
  4. data/.github/workflows/lint.yml +2 -4
  5. data/.github/workflows/ruby.yml +1 -1
  6. data/.gitignore +3 -0
  7. data/.rubocop.yml +6 -1
  8. data/.yardopts +7 -0
  9. data/CHANGELOG.md +22 -0
  10. data/Gemfile +12 -0
  11. data/README.md +1 -1
  12. data/bench/benchmark.rb +34 -0
  13. data/bench/benchmarks/basic_field_serializer.rb +21 -0
  14. data/bench/benchmarks/view_selection.rb +47 -0
  15. data/bin/console +30 -10
  16. data/docs/reporting.md +190 -0
  17. data/docs/serializers.md +4 -1
  18. data/example/Gemfile +2 -2
  19. data/example/Gemfile.lock +116 -123
  20. data/example/app/controllers/application_controller.rb +4 -0
  21. data/example/app/controllers/people_controller.rb +44 -28
  22. data/example/app/output_objects/identified_output.rb +7 -0
  23. data/example/app/output_objects/person_output_object.rb +37 -11
  24. data/example/app/output_objects/post_output_object.rb +0 -4
  25. data/example/app/output_objects/reporting_post_output.rb +18 -0
  26. data/example/bin/rspec +29 -0
  27. data/example/config/environments/production.rb +1 -1
  28. data/example/spec/requests/people/create_spec.rb +3 -2
  29. data/example/spec/requests/people/index_spec.rb +1 -1
  30. data/lib/sober_swag/compiler/path.rb +45 -4
  31. data/lib/sober_swag/compiler/paths.rb +20 -0
  32. data/lib/sober_swag/compiler/primitive.rb +17 -0
  33. data/lib/sober_swag/compiler/type.rb +105 -22
  34. data/lib/sober_swag/compiler.rb +87 -15
  35. data/lib/sober_swag/controller/route.rb +147 -28
  36. data/lib/sober_swag/controller.rb +57 -17
  37. data/lib/sober_swag/input_object.rb +124 -7
  38. data/lib/sober_swag/nodes/array.rb +19 -0
  39. data/lib/sober_swag/nodes/attribute.rb +45 -4
  40. data/lib/sober_swag/nodes/base.rb +27 -7
  41. data/lib/sober_swag/nodes/binary.rb +30 -13
  42. data/lib/sober_swag/nodes/enum.rb +16 -1
  43. data/lib/sober_swag/nodes/list.rb +20 -0
  44. data/lib/sober_swag/nodes/nullable_primitive.rb +3 -0
  45. data/lib/sober_swag/nodes/object.rb +4 -1
  46. data/lib/sober_swag/nodes/one_of.rb +11 -3
  47. data/lib/sober_swag/nodes/primitive.rb +34 -2
  48. data/lib/sober_swag/nodes/sum.rb +8 -0
  49. data/lib/sober_swag/output_object/definition.rb +57 -1
  50. data/lib/sober_swag/output_object/field.rb +31 -11
  51. data/lib/sober_swag/output_object/field_syntax.rb +19 -3
  52. data/lib/sober_swag/output_object/view.rb +46 -1
  53. data/lib/sober_swag/output_object.rb +40 -19
  54. data/lib/sober_swag/parser.rb +7 -1
  55. data/lib/sober_swag/reporting/compiler.rb +39 -0
  56. data/lib/sober_swag/reporting/input/base.rb +11 -0
  57. data/lib/sober_swag/reporting/input/bool.rb +19 -0
  58. data/lib/sober_swag/reporting/input/converting/bool.rb +24 -0
  59. data/lib/sober_swag/reporting/input/converting/date.rb +30 -0
  60. data/lib/sober_swag/reporting/input/converting/date_time.rb +28 -0
  61. data/lib/sober_swag/reporting/input/converting/decimal.rb +24 -0
  62. data/lib/sober_swag/reporting/input/converting/integer.rb +19 -0
  63. data/lib/sober_swag/reporting/input/converting.rb +16 -0
  64. data/lib/sober_swag/reporting/input/defer.rb +29 -0
  65. data/lib/sober_swag/reporting/input/described.rb +38 -0
  66. data/lib/sober_swag/reporting/input/dictionary.rb +37 -0
  67. data/lib/sober_swag/reporting/input/either.rb +51 -0
  68. data/lib/sober_swag/reporting/input/enum.rb +44 -0
  69. data/lib/sober_swag/reporting/input/format.rb +39 -0
  70. data/lib/sober_swag/reporting/input/interface.rb +87 -0
  71. data/lib/sober_swag/reporting/input/list.rb +44 -0
  72. data/lib/sober_swag/reporting/input/mapped.rb +36 -0
  73. data/lib/sober_swag/reporting/input/merge_objects.rb +72 -0
  74. data/lib/sober_swag/reporting/input/null.rb +34 -0
  75. data/lib/sober_swag/reporting/input/number.rb +19 -0
  76. data/lib/sober_swag/reporting/input/object/property.rb +53 -0
  77. data/lib/sober_swag/reporting/input/object.rb +100 -0
  78. data/lib/sober_swag/reporting/input/pattern.rb +46 -0
  79. data/lib/sober_swag/reporting/input/referenced.rb +38 -0
  80. data/lib/sober_swag/reporting/input/struct.rb +271 -0
  81. data/lib/sober_swag/reporting/input/text.rb +42 -0
  82. data/lib/sober_swag/reporting/input.rb +54 -0
  83. data/lib/sober_swag/reporting/invalid_schema_error.rb +21 -0
  84. data/lib/sober_swag/reporting/output/base.rb +25 -0
  85. data/lib/sober_swag/reporting/output/bool.rb +25 -0
  86. data/lib/sober_swag/reporting/output/defer.rb +69 -0
  87. data/lib/sober_swag/reporting/output/described.rb +42 -0
  88. data/lib/sober_swag/reporting/output/dictionary.rb +46 -0
  89. data/lib/sober_swag/reporting/output/interface.rb +83 -0
  90. data/lib/sober_swag/reporting/output/list.rb +54 -0
  91. data/lib/sober_swag/reporting/output/merge_objects.rb +97 -0
  92. data/lib/sober_swag/reporting/output/null.rb +25 -0
  93. data/lib/sober_swag/reporting/output/number.rb +25 -0
  94. data/lib/sober_swag/reporting/output/object/property.rb +45 -0
  95. data/lib/sober_swag/reporting/output/object.rb +54 -0
  96. data/lib/sober_swag/reporting/output/partitioned.rb +77 -0
  97. data/lib/sober_swag/reporting/output/pattern.rb +50 -0
  98. data/lib/sober_swag/reporting/output/referenced.rb +42 -0
  99. data/lib/sober_swag/reporting/output/struct.rb +262 -0
  100. data/lib/sober_swag/reporting/output/text.rb +25 -0
  101. data/lib/sober_swag/reporting/output/via_map.rb +67 -0
  102. data/lib/sober_swag/reporting/output/viewed.rb +72 -0
  103. data/lib/sober_swag/reporting/output.rb +54 -0
  104. data/lib/sober_swag/reporting/report/base.rb +57 -0
  105. data/lib/sober_swag/reporting/report/either.rb +36 -0
  106. data/lib/sober_swag/reporting/report/error.rb +15 -0
  107. data/lib/sober_swag/reporting/report/list.rb +28 -0
  108. data/lib/sober_swag/reporting/report/merged_object.rb +25 -0
  109. data/lib/sober_swag/reporting/report/object.rb +29 -0
  110. data/lib/sober_swag/reporting/report/output.rb +14 -0
  111. data/lib/sober_swag/reporting/report/value.rb +28 -0
  112. data/lib/sober_swag/reporting/report.rb +16 -0
  113. data/lib/sober_swag/reporting.rb +11 -0
  114. data/lib/sober_swag/serializer/array.rb +27 -3
  115. data/lib/sober_swag/serializer/base.rb +75 -25
  116. data/lib/sober_swag/serializer/conditional.rb +33 -1
  117. data/lib/sober_swag/serializer/field_list.rb +23 -5
  118. data/lib/sober_swag/serializer/hash.rb +53 -0
  119. data/lib/sober_swag/serializer/mapped.rb +10 -1
  120. data/lib/sober_swag/serializer/optional.rb +18 -1
  121. data/lib/sober_swag/serializer/primitive.rb +3 -0
  122. data/lib/sober_swag/serializer.rb +1 -0
  123. data/lib/sober_swag/server.rb +27 -11
  124. data/lib/sober_swag/type/named.rb +14 -0
  125. data/lib/sober_swag/types/comma_array.rb +4 -0
  126. data/lib/sober_swag/version.rb +1 -1
  127. data/lib/sober_swag.rb +7 -1
  128. metadata +72 -2
@@ -0,0 +1,39 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Specify that something must match a particular format.
6
+ # Note: said format is just a string.
7
+ class Format < Base
8
+ def initialize(input, format)
9
+ @input = input
10
+ @format = format
11
+ end
12
+
13
+ ##
14
+ # @return [Interface]
15
+ attr_reader :input
16
+
17
+ ##
18
+ # @return [String]
19
+ attr_reader :format
20
+
21
+ def call(object)
22
+ input.call(object)
23
+ end
24
+
25
+ def swagger_schema
26
+ schema, found = input.swagger_schema
27
+
28
+ merged =
29
+ if schema.key?(:$ref)
30
+ { allOf: [schema] }
31
+ else
32
+ schema
33
+ end.merge(format: format)
34
+ [merged, found]
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,87 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Module for interface methods.
6
+ module Interface
7
+ ##
8
+ # Make a new input that is either this type or the argument.
9
+ #
10
+ # @argument other [Interface] other input type
11
+ # @return [Either] this input, or some other input.
12
+ def or(other)
13
+ Either.new(self, other)
14
+ end
15
+
16
+ ##
17
+ # @see {#or}
18
+ def |(other)
19
+ Either.new(self, other)
20
+ end
21
+
22
+ ##
23
+ # This, or null.
24
+ #
25
+ # @return [Either] an either type of this or nil.
26
+ def optional
27
+ self | Null.new
28
+ end
29
+
30
+ ##
31
+ # A list of this input.
32
+ #
33
+ # @return [List] the new input.
34
+ def list
35
+ List.new(self)
36
+ end
37
+
38
+ def referenced(name)
39
+ Referenced.new(self, name)
40
+ end
41
+
42
+ def format(format)
43
+ Format.new(self, format)
44
+ end
45
+
46
+ def described(desc)
47
+ Described.new(self, desc)
48
+ end
49
+
50
+ def enum(*cases)
51
+ Enum.new(self, cases)
52
+ end
53
+
54
+ ##
55
+ # Map a function after this input runs.
56
+ #
57
+ # @return [Mapped] the new input.
58
+ def mapped(&block)
59
+ Mapped.new(self, block)
60
+ end
61
+
62
+ def call!(value)
63
+ res = call(value)
64
+ raise Report::Error.new(res) if res.is_a?(Report::Base) # rubocop:disable Style/RaiseArgs
65
+
66
+ res
67
+ end
68
+
69
+ def swagger_path_schema
70
+ raise InvalidSchemaError::InvalidForPathError.new(self) # rubocop:disable Style/RaiseArgs
71
+ end
72
+
73
+ def swagger_query_schema
74
+ raise InvalidSchemaError::InvalidForQueryError.new(self) # rubocop:disable Style/RaiseArgs
75
+ end
76
+
77
+ def add_schema_key(base, addition)
78
+ if base.key?(:$ref)
79
+ { allOf: [base] }.merge(addition)
80
+ else
81
+ base.merge(addition)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,44 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Class to parse an array, where each element has the same type.
6
+ #
7
+ # Called List to avoid name conflicts.
8
+ class List < Base
9
+ ##
10
+ # @param element [Base] the parser for elements
11
+ def initialize(element)
12
+ @element = element
13
+ end
14
+
15
+ ##
16
+ # @return [Base] the parser for elements
17
+ attr_reader :element
18
+
19
+ def call(value)
20
+ return Report::Value.new(['was not an array']) unless value.is_a?(Array)
21
+
22
+ # obtain a hash of indexes => errors
23
+ errs = {}
24
+ # yes, side effects in a map are evil, but we avoid traversal twice
25
+ mapped = value.map.with_index do |item, idx|
26
+ element.call(item).tap { |e| errs[idx] = e if e.is_a?(Report::Base) }
27
+ end
28
+
29
+ if errs.any?
30
+ Report::List.new(errs)
31
+ else
32
+ mapped
33
+ end
34
+ end
35
+
36
+ def swagger_schema
37
+ schema, found = element.swagger_schema
38
+
39
+ [{ type: 'list', items: schema }, found]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Apply a mapping function over an input.
6
+ class Mapped < Base
7
+ ##
8
+ # @param mapper [#call] the mapping function
9
+ # @param input [Base] the base input
10
+ def initialize(input, mapper)
11
+ @mapper = mapper
12
+ @input = input
13
+ end
14
+
15
+ ##
16
+ # @return [#call] mapping function
17
+ attr_reader :mapper
18
+ ##
19
+ # @return [Base] base input
20
+ attr_reader :input
21
+
22
+ def call(value)
23
+ val = input.call(value)
24
+
25
+ return val if val.is_a?(Report::Base)
26
+
27
+ mapper.call(val)
28
+ end
29
+
30
+ def swagger_schema
31
+ input.swagger_schema
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,72 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Merge two object types together, in an allof stype relationship
6
+ class MergeObjects < Base
7
+ def initialize(parent, child)
8
+ @parent = parent
9
+ @child = child
10
+ end
11
+
12
+ ##
13
+ # @return [Interface] parent type
14
+ attr_reader :parent
15
+
16
+ ##
17
+ # @return [Interface] child type
18
+ attr_reader :child
19
+
20
+ def call(value)
21
+ parent_attrs = parent.call(value)
22
+
23
+ return parent_attrs if parent_attrs.is_a?(Report::Value)
24
+
25
+ # otherwise, object type, so we want to get a full error report
26
+
27
+ child_attrs = child.call(value)
28
+
29
+ return child_attrs if child_attrs.is_a?(Report::Value)
30
+
31
+ merge_results(parent_attrs, child_attrs)
32
+ end
33
+
34
+ def swagger_schema
35
+ parent_schema, parent_found = parent.swagger_schema
36
+ child_schema, child_found = child.swagger_schema
37
+
38
+ [
39
+ {
40
+ allOf: (parent_schema[:allOf] || [parent_schema]) + (child_schema[:allOf] || [child_schema])
41
+ },
42
+ parent_found.merge(child_found)
43
+ ]
44
+ end
45
+
46
+ def swagger_path_schema
47
+ parent.swagger_path_schema + child.swagger_path_schema
48
+ end
49
+
50
+ def swagger_query_schema
51
+ parent.swagger_query_schema + child.swagger_query_schema
52
+ end
53
+
54
+ private
55
+
56
+ def merge_results(par, chi) # rubocop:disable Metrics/MethodLength
57
+ if par.is_a?(Report::Base)
58
+ if chi.is_a?(Report::Base)
59
+ Report::MergedObject.new(par, chi)
60
+ else
61
+ par
62
+ end
63
+ elsif chi.is_a?(Report::Base)
64
+ chi
65
+ else
66
+ par.to_h.merge(chi.to_h)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,34 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Null input values.
6
+ # Validates that the input is null.
7
+ class Null < Base
8
+ def call(value)
9
+ return nil if value.nil?
10
+
11
+ Report::Value.new(['was not nil'])
12
+ end
13
+
14
+ def hash
15
+ [self.class.hash, 1].hash
16
+ end
17
+
18
+ def eql?(other)
19
+ other.class == self.class
20
+ end
21
+
22
+ def <=>(other)
23
+ eql?(other) ? 0 : nil
24
+ end
25
+
26
+ include Comparable
27
+
28
+ def swagger_schema
29
+ [{ type: 'null' }, {}]
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Parse some kind of number.
6
+ class Number < Base
7
+ def call(input)
8
+ return Report::Value.new(['is not a number']) unless input.is_a?(Numeric)
9
+
10
+ input
11
+ end
12
+
13
+ def swagger_schema
14
+ [{ type: 'number' }, {}]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,53 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ class Object
5
+ ##
6
+ # Describe a single property key in an object.
7
+ class Property
8
+ def initialize(value, required:, description: '')
9
+ @value = value
10
+ @required = required
11
+ @description = description
12
+ end
13
+
14
+ ##
15
+ # @return [SoberSwag::Reporting::Input::Interface] value type
16
+ attr_reader :value
17
+
18
+ def required?
19
+ @required
20
+ end
21
+
22
+ ##
23
+ # @return [String, nil] description
24
+ attr_reader :description
25
+
26
+ def property_schema
27
+ direct, refined = value.swagger_schema
28
+
29
+ if description
30
+ [add_description(direct), refined]
31
+ else
32
+ [direct, refined]
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def add_description(dir)
39
+ t =
40
+ if dir.key?(:$ref)
41
+ # workaround: we have to do this if we want to allow
42
+ # descriptions in reference types
43
+ { allOf: [dir] }
44
+ else
45
+ dir
46
+ end
47
+ t.merge(description: description)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,100 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Input object values
6
+ class Object < Base
7
+ autoload :Property, 'sober_swag/reporting/input/object/property'
8
+ ##
9
+ # @param fields [Hash<Symbol, Property>]
10
+ def initialize(fields)
11
+ @fields = fields
12
+ end
13
+
14
+ ##
15
+ # @return [Hash<String,#call>]
16
+ attr_reader :fields
17
+
18
+ def call(value)
19
+ return Report::Value.new(['was a not a JSON object']) unless value.is_a?(Hash)
20
+
21
+ bad, good = fields.map { |k, prop|
22
+ extract_value(k, prop, value)
23
+ }.compact.partition { |(_, v)| v.is_a?(Report::Base) }
24
+
25
+ return Report::Object.new(bad.to_h) if bad.any?
26
+
27
+ good.to_h
28
+ end
29
+
30
+ def swagger_schema
31
+ fields, found = field_schemas
32
+
33
+ obj = {
34
+ type: 'object',
35
+ properties: fields
36
+ }.merge(required_portion)
37
+
38
+ [obj, found]
39
+ end
40
+
41
+ def swagger_query_schema
42
+ swagger_parameter_schema.map do |param|
43
+ param.merge({ in: :query, style: :deepObject, explode: true })
44
+ end
45
+ end
46
+
47
+ def swagger_path_schema
48
+ swagger_parameter_schema.map do |param|
49
+ param.merge({ in: :path })
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def swagger_parameter_schema
56
+ fields.map do |name, field|
57
+ key_schema, = field.property_schema
58
+ base = {
59
+ name: name,
60
+ schema: key_schema,
61
+ required: field.required?
62
+ }
63
+ field.description ? base.merge(description: field.description) : base
64
+ end
65
+ end
66
+
67
+ def field_schemas
68
+ fields.reduce([{}, Set.new]) do |(field_schemas, found), (k, v)|
69
+ key_schema, key_found = v.property_schema
70
+ [
71
+ field_schemas.merge(k => key_schema),
72
+ found.merge(key_found)
73
+ ]
74
+ end
75
+ end
76
+
77
+ ##
78
+ # Either the list of required keys, or something stating "provide at least one key."
79
+ # This is needed because you can't have an empty list of keys.
80
+ def required_portion
81
+ required_fields = fields.map { |k, v| k if v.required? }.compact
82
+
83
+ if required_fields.empty?
84
+ {}
85
+ else
86
+ { required: required_fields }
87
+ end
88
+ end
89
+
90
+ def extract_value(key, property, input)
91
+ if input.key?(key)
92
+ [key, property.value.call(input[key])]
93
+ elsif property.required?
94
+ [key, Report::Value.new(['is required'])]
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,46 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Input values that validate against a pattern
6
+ class Pattern < Base
7
+ def initialize(input, pattern)
8
+ @input = input
9
+ @pattern = pattern
10
+ end
11
+
12
+ ##
13
+ # @return [#call] input type
14
+ attr_reader :input
15
+
16
+ ##
17
+ # @return [#matches] regexp matcher
18
+ attr_reader :pattern
19
+
20
+ def call(value)
21
+ val = input.call(value)
22
+
23
+ return val if val.is_a?(Report::Base)
24
+
25
+ if pattern.match?(value)
26
+ value
27
+ else
28
+ Report::Value.new(["did not match pattern #{pattern}"])
29
+ end
30
+ end
31
+
32
+ def swagger_schema
33
+ single, found = input.swagger_schema
34
+
35
+ [add_schema_key(single, { pattern: formatted_pattern }), found]
36
+ end
37
+
38
+ ##
39
+ # Try to format a pattern so it'll work nicely with JS.
40
+ def formatted_pattern
41
+ pattern.to_s.gsub('?-mix:', '')
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,38 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # An input that should be "referenced" in the final schema.
6
+ class Referenced < Base
7
+ def initialize(value, reference)
8
+ @value = value
9
+ @reference = reference
10
+ end
11
+
12
+ ##
13
+ # @return [Interface] the actual input
14
+ attr_reader :value
15
+ ##
16
+ # @return [String] key in the components hash
17
+ attr_reader :reference
18
+
19
+ def call(input)
20
+ @value.call(input)
21
+ end
22
+
23
+ def swagger_schema
24
+ [
25
+ { "$ref": ref_path },
26
+ { reference => proc { value.swagger_schema } }
27
+ ]
28
+ end
29
+
30
+ private
31
+
32
+ def ref_path
33
+ "#/components/schemas/#{reference}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end