Skip to content

Commit 1ebe762

Browse files
committed
Improve Swagger base URL parsing and support openAPI 3.0
1 parent df13e46 commit 1ebe762

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

tests/data/openapi3.yaml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Fake Swagger
5+
license:
6+
name: MIT
7+
servers:
8+
- url: https://{customerId}.openapi.fr:{port}/v1
9+
variables:
10+
customerId:
11+
default: fake
12+
description: Customer ID assigned by the service provider
13+
port:
14+
enum:
15+
- '443'
16+
- '8443'
17+
- '8080'
18+
default: '8080'
19+
paths:
20+
/pets:
21+
get:
22+
summary: List all pets
23+
operationId: listPets
24+
tags:
25+
- pets
26+
parameters:
27+
- name: limit
28+
in: query
29+
description: How many items to return at one time (max 100)
30+
required: false
31+
schema:
32+
type: integer
33+
format: int32
34+
responses:
35+
200:
36+
description: An paged array of pets
37+
headers:
38+
x-next:
39+
description: A link to the next page of responses
40+
schema:
41+
type: string
42+
content:
43+
application/json:
44+
schema:
45+
$ref: "#/components/schemas/Pets"
46+
default:
47+
description: unexpected error
48+
content:
49+
application/json:
50+
schema:
51+
$ref: "#/components/schemas/Error"
52+
post:
53+
summary: Create a pet
54+
operationId: createPets
55+
tags:
56+
- pets
57+
responses:
58+
201:
59+
description: Null response
60+
default:
61+
description: unexpected error
62+
content:
63+
application/json:
64+
schema:
65+
$ref: "#/components/schemas/Error"
66+
/pets/{petId}:
67+
get:
68+
summary: Info for a specific pet
69+
operationId: showPetById
70+
tags:
71+
- pets
72+
parameters:
73+
- name: petId
74+
in: path
75+
required: true
76+
description: The id of the pet to retrieve
77+
schema:
78+
type: string
79+
responses:
80+
200:
81+
description: Expected response to a valid request
82+
content:
83+
application/json:
84+
schema:
85+
$ref: "#/components/schemas/Pets"
86+
default:
87+
description: unexpected error
88+
content:
89+
application/json:
90+
schema:
91+
$ref: "#/components/schemas/Error"
92+
components:
93+
schemas:
94+
Pet:
95+
required:
96+
- id
97+
- name
98+
properties:
99+
id:
100+
type: integer
101+
format: int64
102+
name:
103+
type: string
104+
tag:
105+
type: string
106+
Pets:
107+
type: array
108+
items:
109+
$ref: "#/components/schemas/Pet"
110+
Error:
111+
required:
112+
- code
113+
- message
114+
properties:
115+
code:
116+
type: integer
117+
format: int32
118+
message:
119+
type: string

tests/parsers/test_swagger_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,13 @@ def test_openapi_yaml_file():
151151
"https://fake.openapi.fr/eval?s=default",
152152
"https://fake.openapi.fr/help"
153153
} == {x.url for x in page.get_requests()}
154+
155+
def test_openapi3():
156+
url = "tests/data/openapi3.yaml"
157+
page = Swagger(base_url="https://fake.openapi.fr", swagger_url=url)
158+
159+
assert{
160+
"https://fake.openapi.fr:8080/v1/pets?limit=1337",
161+
"https://fake.openapi.fr:8080/v1/pets",
162+
"https://fake.openapi.fr:8080/v1/pets/default"
163+
} == {x.url for x in page.get_requests()}

wapitiCore/parsers/swagger.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ def __init__(self, swagger_url: str = None, base_url: str = None) -> None:
6767
def _get_base_url(swagger_dict: dict, url: str) -> str:
6868
try:
6969
parsed_host = urllib.parse.urlparse(url)
70+
if 'servers' in swagger_dict:
71+
for server in swagger_dict['servers']:
72+
if 'url' in server and server['url'] != "":
73+
if server['url'].endswith("/"):
74+
server['url'] = server['url'][:-1]
75+
swagger_url = server['url']
76+
if 'variables' in server:
77+
for variable in server['variables']:
78+
swagger_url = swagger_url.replace(
79+
"{" + variable + "}", server['variables'][variable]['default']
80+
)
81+
swagger_dict['basePath'] = ""
82+
swagger_dict['host'] = swagger_url
83+
return swagger_url
7084
if 'schemes' not in swagger_dict:
7185
# get http or https from url
7286
swagger_dict['schemes'] = parsed_host.scheme

0 commit comments

Comments
 (0)