-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathopenapi_spec.rb
More file actions
174 lines (157 loc) · 5.33 KB
/
openapi_spec.rb
File metadata and controls
174 lines (157 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# frozen_string_literal: true
RSpec.describe Openapi3Parser::NodeFactory::Openapi do
let(:minimal_openapi_definition) do
{
"openapi" => "3.0.0",
"info" => {
"title" => "Minimal Openapi definition",
"version" => "1.0.0"
},
"paths" => {}
}
end
it_behaves_like "node object factory", Openapi3Parser::Node::Openapi do
let(:input) { minimal_openapi_definition }
end
context "when input is nil" do
let(:factory_context) { create_node_factory_context(nil) }
it "is invalid" do
instance = described_class.new(factory_context)
expect(instance).not_to be_valid
expect(instance)
.to have_validation_error("#/")
.with_message("Invalid type. Expected Object")
end
it "raises an error trying to access the node" do
instance = described_class.new(factory_context)
node_context = node_factory_context_to_node_context(factory_context)
expect { instance.node(node_context) }.to raise_error(Openapi3Parser::Error)
end
end
describe "validating tags" do
it "is valid when tags contain no duplicates" do
factory_context = create_node_factory_context(
minimal_openapi_definition.merge(
"tags" => [{ "name" => "a" }, { "name" => "b" }]
)
)
expect(described_class.new(factory_context)).to be_valid
end
it "is invalid for an invalid key" do
factory_context = create_node_factory_context(
minimal_openapi_definition.merge(
"tags" => [{ "name" => "a" }, { "name" => "a" }]
)
)
instance = described_class.new(factory_context)
expect(instance).not_to be_valid
expect(instance)
.to have_validation_error("#/tags")
.with_message("Duplicate tag names: a")
end
end
describe "validating operationIds" do
it "is valid when operationIds are unique" do
factory_context = create_node_factory_context(
minimal_openapi_definition.merge(
"paths" => {
"/pets" => {
"get" => {
"operationId" => "listPets",
"responses" => { "200" => { "description" => "Success" } }
}
},
"/pets/{id}" => {
"get" => {
"operationId" => "getPet",
"responses" => { "200" => { "description" => "Success" } }
}
}
}
)
)
expect(described_class.new(factory_context)).to be_valid
end
it "is valid when some operations have no operationId" do
factory_context = create_node_factory_context(
minimal_openapi_definition.merge(
"paths" => {
"/pets" => {
"get" => {
"operationId" => "listPets",
"responses" => { "200" => { "description" => "Success" } }
}
},
"/pets/{id}" => {
"get" => {
"responses" => { "200" => { "description" => "Success" } }
}
}
}
)
)
expect(described_class.new(factory_context)).to be_valid
end
it "is invalid when operationIds are duplicated" do
factory_context = create_node_factory_context(
minimal_openapi_definition.merge(
"paths" => {
"/pets" => {
"get" => {
"operationId" => "getPets",
"responses" => { "200" => { "description" => "Success" } }
}
},
"/pets/{id}" => {
"get" => {
"operationId" => "getPets",
"responses" => { "200" => { "description" => "Success" } }
}
}
}
)
)
instance = described_class.new(factory_context)
expect(instance).not_to be_valid
expect(instance)
.to have_validation_error("#/paths")
.with_message("Duplicate operationId values: getPets")
end
end
describe "default values for servers" do
it "contains a basic root server when servers input is nil" do
node = create_node(minimal_openapi_definition.merge({ "servers" => nil }))
expect(node["servers"].length).to be 1
expect(node["servers"][0].url).to eq "/"
expect(node["servers"][0].description).to be_nil
end
it "contains a basic root server when servers input is an empty array" do
node = create_node(minimal_openapi_definition.merge({ "servers" => [] }))
expect(node["servers"].length).to be 1
expect(node["servers"][0].url).to eq "/"
expect(node["servers"][0].description).to be_nil
end
it "uses the defined servers when they are provided" do
node = create_node(
minimal_openapi_definition.merge(
{
"servers" => [
{
"url" => "https://prod.example.com/v1",
"description" => "Production server"
}
]
}
)
)
expect(node["servers"][0].url).to eq "https://prod.example.com/v1"
expect(node["servers"][0].description).to eq "Production server"
end
end
def create_node(input)
node_factory_context = create_node_factory_context(input)
instance = described_class.new(node_factory_context)
node_context = node_factory_context_to_node_context(node_factory_context)
instance.node(node_context)
end
end