Skip to content

Commit 39f5426

Browse files
authored
Fix spec linking with different subfolders (#196)
Co-authored-by: Kai Harder <[email protected]>
1 parent 0c1f43f commit 39f5426

File tree

12 files changed

+284
-12
lines changed

12 files changed

+284
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 5.0.2
4+
5+
* Fixed an issue causing a spec to be linked incorrectly when the source document is in a subfolder
6+
but the referenced spec is in another one
7+
38
## 5.0.1
49

510
* Fix some errors when the spec is in a subdirectory

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "swagger-plugin-for-sphinx"
7-
version = "5.0.1"
7+
version = "5.0.2"
88
description = "Sphinx plugin which renders a OpenAPI specification with Swagger"
99
authors = [{ name = "Kai Harder", email = "[email protected]" }]
1010
readme = "README.md"

swagger_plugin_for_sphinx/_plugin.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,21 @@ def run(self) -> list[nodes.Node]:
4646
f"{app.env.doc2path(app.env.docname)}:{self.lineno}."
4747
)
4848

49-
relpath, abspath = self.env.relfn2path(self.arguments[0])
49+
_, abspath = self.env.relfn2path(self.arguments[0])
5050
spec = Path(abspath).resolve()
5151
if not spec.exists():
5252
raise ExtensionError(
5353
f"In file '{app.env.doc2path(app.env.docname)}:{self.lineno}', "
5454
f"file not found: {self.arguments[0]}."
5555
)
5656

57-
rel_parts = Path(relpath).parts
5857
logger.info(f"Adding to html_static_path: {spec}.")
5958
app.config.html_static_path.extend([spec])
6059

6160
url_path = (
62-
"../".join(["" for _ in range(len(rel_parts))])
61+
"../".join(["" for _ in range(len(app.env.docname.split("/")))])
6362
+ "_static/"
64-
+ self.arguments[0]
63+
+ spec.name
6564
)
6665

6766
config = {

tests/test_data/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ Sample Docu
66

77
overview
88
openapi
9+
subfolder/p1
10+
subfolder/p2

tests/test_data/openapi.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
OpenAPI Specification
22
=====================
33

4-
.. swagger-plugin:: openapi.yml
4+
.. swagger-plugin:: openapi_main.yml
55
:full-page:
66
:page-title: Swagger Petstore

tests/test_data/openapi.yml renamed to tests/test_data/openapi_main.yml

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

tests/test_data/subfolder/p1.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
OpenAPI Specification for Subfolder 1
2+
=====================================
3+
4+
.. swagger-plugin:: openapi_subfolder.yml
5+
:id: openapi_subfolder
6+
7+
.. swagger-plugin:: ../openapi_main.yml
8+
:id: openapi_main

tests/test_data/subfolder/p2.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OpenAPI Specification for Subfolder 2
2+
=====================================
3+
4+
.. swagger-plugin:: ../specs/openapi_specs.yml
5+
:full-page:

0 commit comments

Comments
 (0)