Skip to content

Commit 9cded99

Browse files
authored
Mark query params and request body attributes as required (#20)
Where possible we now mark the following as required: - query params - request body attributes - args defined in argument sets closes: #3
1 parent 0a51c84 commit 9cded99

File tree

8 files changed

+53
-17
lines changed

8 files changed

+53
-17
lines changed

examples/core_api/argument_sets/time_lookup_argument_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module CoreAPI
44
module ArgumentSets
55
class TimeLookupArgumentSet < Apia::LookupArgumentSet
66

7-
argument :unix, type: :string
7+
argument :unix, type: :string, required: true
88
argument :string, type: :string
99

1010
potential_error "InvalidTime" do

examples/core_api/controllers/time_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TimeController < Apia::Controller
1818
endpoint :format do
1919
description "Format the given time"
2020
argument :time, type: ArgumentSets::TimeLookupArgumentSet, required: true
21-
argument :timezone, type: Objects::TimeZone
21+
argument :timezone, type: Objects::TimeZone, required: true
2222
field :formatted_time, type: :string, null: true
2323
action do
2424
time = request.arguments[:time]

examples/core_api/endpoints/test_endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TestEndpoint < Apia::Endpoint
88

99
description "Returns the current time"
1010
argument :object, type: ArgumentSets::ObjectLookup, required: true
11-
argument :scalar, type: :string
11+
argument :scalar, type: :string, required: true
1212
field :time, type: Objects::Time, include: "unix,day_of_week,year[as_string]", null: true do
1313
condition do |o|
1414
o[:time].year.to_s == "2023"

examples/core_api/endpoints/time_now_endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TimeNowEndpoint < Apia::Endpoint
88

99
description "Returns the current time"
1010
argument :timezone, type: Objects::TimeZone
11-
argument :time_zones, [Objects::TimeZone]
11+
argument :time_zones, [Objects::TimeZone], required: true
1212
argument :filters, [:string]
1313
field :time, type: Objects::Time
1414
field :time_zones, type: [Objects::TimeZone]

lib/apia/open_api/objects/parameters.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ def add_to_spec
6161
items: items
6262
}
6363
}
64+
param[:required] = true if @argument.required?
6465
add_to_parameters(param)
6566
elsif @argument.type.enum?
6667
param = {
6768
name: @argument.name.to_s,
6869
in: "query",
6970
schema: generate_schema_ref(@argument.type.klass.definition)
7071
}
72+
param[:required] = true if @argument.required?
7173
add_to_parameters(param)
7274
add_to_components_schemas(@argument)
7375
else
@@ -78,6 +80,7 @@ def add_to_spec
7880
type: convert_type_to_open_api_data_type(@argument.type)
7981
}
8082
}
83+
param[:required] = true if @argument.required?
8184
add_to_parameters(param)
8285
end
8386
end

lib/apia/open_api/objects/request_body.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def initialize(spec:, route:, route_spec:)
3131
end
3232

3333
def add_to_spec
34+
required = []
3435
@route.endpoint.definition.argument_set.definition.arguments.each_value do |arg|
36+
required << arg.name.to_s if arg.required?
3537
if arg.array?
3638
if arg.type.argument_set? || arg.type.enum?
3739
items = generate_schema_ref(arg.type.klass.definition)
@@ -54,12 +56,13 @@ def add_to_spec
5456
end
5557
end
5658

59+
schema = { properties: @properties }
60+
schema[:required] = required unless required.empty?
61+
5762
@route_spec[:requestBody] = {
5863
content: {
5964
"application/json": {
60-
schema: {
61-
properties: @properties
62-
}
65+
schema: schema
6366
}
6467
}
6568
}

lib/apia/open_api/objects/schema.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def generate_schema_for_child(schema, child, all_properties_included)
120120
type: convert_type_to_open_api_data_type(child.type)
121121
}
122122
end
123+
124+
if child.try(:required?)
125+
schema[:required] ||= []
126+
schema[:required] << child.name.to_s
127+
end
123128
schema
124129
end
125130

spec/support/fixtures/openapi.json

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"in": "query",
3737
"schema": {
3838
"$ref": "#/components/schemas/CoreAPI_Objects_TimeZone"
39-
}
39+
},
40+
"required": true
4041
}
4142
],
4243
"responses": {
@@ -78,7 +79,11 @@
7879
"timezone": {
7980
"$ref": "#/components/schemas/CoreAPI_Objects_TimeZone"
8081
}
81-
}
82+
},
83+
"required": [
84+
"time",
85+
"timezone"
86+
]
8287
}
8388
}
8489
}
@@ -122,7 +127,10 @@
122127
"$ref": "#/components/schemas/CoreAPI_ArgumentSets_TimeLookupArgumentSet"
123128
}
124129
}
125-
}
130+
},
131+
"required": [
132+
"times"
133+
]
126134
}
127135
}
128136
}
@@ -204,7 +212,8 @@
204212
"items": {
205213
"$ref": "#/components/schemas/CoreAPI_Objects_TimeZone"
206214
}
207-
}
215+
},
216+
"required": true
208217
},
209218
{
210219
"name": "filters[]",
@@ -298,7 +307,10 @@
298307
"type": "string"
299308
}
300309
}
301-
}
310+
},
311+
"required": [
312+
"time_zones"
313+
]
302314
}
303315
}
304316
}
@@ -386,7 +398,8 @@
386398
"in": "query",
387399
"schema": {
388400
"type": "string"
389-
}
401+
},
402+
"required": true
390403
}
391404
],
392405
"responses": {
@@ -445,7 +458,11 @@
445458
"scalar": {
446459
"type": "string"
447460
}
448-
}
461+
},
462+
"required": [
463+
"object",
464+
"scalar"
465+
]
449466
}
450467
}
451468
}
@@ -517,7 +534,8 @@
517534
"in": "query",
518535
"schema": {
519536
"$ref": "#/components/schemas/CoreAPI_Objects_TimeZone"
520-
}
537+
},
538+
"required": true
521539
}
522540
],
523541
"responses": {
@@ -557,7 +575,11 @@
557575
"timezone": {
558576
"$ref": "#/components/schemas/CoreAPI_Objects_TimeZone"
559577
}
560-
}
578+
},
579+
"required": [
580+
"time",
581+
"timezone"
582+
]
561583
}
562584
}
563585
}
@@ -604,7 +626,10 @@
604626
"string": {
605627
"type": "string"
606628
}
607-
}
629+
},
630+
"required": [
631+
"unix"
632+
]
608633
},
609634
"CoreAPI_Objects_Time": {
610635
"type": "object",

0 commit comments

Comments
 (0)