Skip to content

Commit 0c66250

Browse files
committed
Add descriptions for LookupArgumentSets
OpenAPI 3.0 does not support parameter dependencies and mutually exclusive parameters. https://swagger.io/docs/specification/describing-parameters/#dependencies This is a complex topic with a lot of debate: OAI/OpenAPI-Specification#256 We know that our LookupArgumentSets are mutually exclusive, so the best thing is to add a description explaining this, so that it will appear in documentation and tools like the swagger editor. Apia already returns a 400 with a friendly error message. closes: #4
1 parent 9cded99 commit 0c66250

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

examples/core_api/argument_sets/object_lookup.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class ObjectLookup < Apia::LookupArgumentSet
88
description "Provides for objects to be looked up"
99

1010
argument :id, type: :string
11-
argument :permalink, type: :string
11+
argument :permalink, type: :string do
12+
description "The permalink of the object to look up"
13+
end
1214

1315
potential_error "ObjectNotFound" do
1416
code :object_not_found

lib/apia/open_api/helpers.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def generate_id_from_definition(definition)
3838
definition.id.gsub(/\//, "_")
3939
end
4040

41+
def formatted_description(description)
42+
return description if description.end_with?(".")
43+
44+
"#{description}."
45+
end
46+
4147
end
4248
end
4349
end

lib/apia/open_api/objects/parameters.rb

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,7 @@ def initialize(spec:, argument:, route_spec:)
3434

3535
def add_to_spec
3636
if @argument.type.argument_set?
37-
# complex argument sets are not supported in query params (e.g. nested objects)
38-
@argument.type.klass.definition.arguments.each_value do |child_arg|
39-
param = {
40-
name: "#{@argument.name}[#{child_arg.name}]",
41-
in: "query",
42-
schema: {
43-
type: convert_type_to_open_api_data_type(child_arg.type)
44-
}
45-
}
46-
add_to_parameters(param)
47-
end
37+
generate_argument_set_params
4838
elsif @argument.array?
4939
if @argument.type.enum? || @argument.type.object?
5040
items = generate_schema_ref(@argument.type.klass.definition)
@@ -87,6 +77,27 @@ def add_to_spec
8777

8878
private
8979

80+
# Complex argument sets are not supported in query params (e.g. nested objects)
81+
# For any LookupArgumentSet only one argument is expected to be provided.
82+
# However, OpenAPI does not currently support describing mutually exclusive query params.
83+
# refer to: https://swagger.io/docs/specification/describing-parameters/#dependencies
84+
def generate_argument_set_params
85+
@argument.type.klass.definition.arguments.each_value do |child_arg|
86+
param = {
87+
name: "#{@argument.name}[#{child_arg.name}]",
88+
in: "query",
89+
schema: {
90+
type: convert_type_to_open_api_data_type(child_arg.type)
91+
}
92+
}
93+
description = []
94+
description << formatted_description(child_arg.description) if child_arg.description.present?
95+
description << "All '#{@argument.name}[]' params are mutually exclusive, only one can be provided."
96+
param[:description] = description.join(" ")
97+
add_to_parameters(param)
98+
end
99+
end
100+
90101
def add_to_parameters(param)
91102
@route_spec[:parameters] << param
92103
end

lib/apia/open_api/objects/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def build_schema_for_polymorph
7070
def generate_child_schemas
7171
if @definition.type.argument_set?
7272
@children = @definition.type.klass.definition.arguments.values
73+
@schema[:description] = "All '#{@definition.name}[]' params are mutually exclusive, only one can be provided."
7374
elsif @definition.type.object?
7475
@children = @definition.type.klass.definition.fields.values
7576
elsif @definition.type.enum?

spec/support/fixtures/openapi.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
"in": "query",
2323
"schema": {
2424
"type": "string"
25-
}
25+
},
26+
"description": "All 'time[]' params are mutually exclusive, only one can be provided."
2627
},
2728
{
2829
"name": "time[string]",
2930
"in": "query",
3031
"schema": {
3132
"type": "string"
32-
}
33+
},
34+
"description": "All 'time[]' params are mutually exclusive, only one can be provided."
3335
},
3436
{
3537
"name": "timezone",
@@ -384,14 +386,16 @@
384386
"in": "query",
385387
"schema": {
386388
"type": "string"
387-
}
389+
},
390+
"description": "All 'object[]' params are mutually exclusive, only one can be provided."
388391
},
389392
{
390393
"name": "object[permalink]",
391394
"in": "query",
392395
"schema": {
393396
"type": "string"
394-
}
397+
},
398+
"description": "The permalink of the object to look up. All 'object[]' params are mutually exclusive, only one can be provided."
395399
},
396400
{
397401
"name": "scalar",
@@ -520,14 +524,16 @@
520524
"in": "query",
521525
"schema": {
522526
"type": "string"
523-
}
527+
},
528+
"description": "All 'time[]' params are mutually exclusive, only one can be provided."
524529
},
525530
{
526531
"name": "time[string]",
527532
"in": "query",
528533
"schema": {
529534
"type": "string"
530-
}
535+
},
536+
"description": "All 'time[]' params are mutually exclusive, only one can be provided."
531537
},
532538
{
533539
"name": "timezone",
@@ -618,6 +624,7 @@
618624
]
619625
},
620626
"CoreAPI_ArgumentSets_TimeLookupArgumentSet": {
627+
"description": "All 'time[]' params are mutually exclusive, only one can be provided.",
621628
"type": "object",
622629
"properties": {
623630
"unix": {
@@ -742,6 +749,7 @@
742749
}
743750
},
744751
"CoreAPI_ArgumentSets_ObjectLookup": {
752+
"description": "All 'object[]' params are mutually exclusive, only one can be provided.",
745753
"type": "object",
746754
"properties": {
747755
"id": {

0 commit comments

Comments
 (0)