File tree 6 files changed +233
-0
lines changed
6 files changed +233
-0
lines changed Original file line number Diff line number Diff line change
1
+ ===================
2
+ Split Specs Example
3
+ ===================
4
+
5
+ This example demonstrates split specifications and relative references.
6
+ The OpenAPI specification and the Swagger specification are stored in
7
+ multiple files and use references that are resolved by Connexion.
8
+
9
+ Preparing
10
+ ---------
11
+
12
+ Create a new virtual environment and install the required libraries
13
+ with these commands:
14
+
15
+ .. code-block :: bash
16
+ $ python -m venv my-venv
17
+ $ source my-venv/bin/activate
18
+ $ pip install ' connexion[flask,swagger-ui,uvicorn]>=3.1.0'
19
+
20
+ Running
21
+ -------
22
+
23
+ Launch the connexion server with this command:
24
+
25
+ Running:
26
+
27
+ .. code-block :: bash
28
+
29
+ $ python app.py
30
+
31
+ Now open your browser and view the Swagger UI for these specification files:
32
+
33
+ * http://localhost:8080/openapi/ui/ for the OpenAPI 3 spec
34
+ * http://localhost:8080/swagger/ui/ for the Swagger 2 spec
Original file line number Diff line number Diff line change
1
+ from pathlib import Path
2
+
3
+ import connexion
4
+
5
+ pets = {
6
+ 1 : {"name" : "Aldo" , "registered" : "2022-11-28T00:00:00Z" },
7
+ 2 : {"name" : "Bailey" , "registered" : "2023-11-28T11:11:11Z" },
8
+ 3 : {"name" : "Hugo" , "registered" : "2024-11-28T22:22:22Z" },
9
+ }
10
+
11
+
12
+ def get (petId ):
13
+ id_ = int (petId )
14
+ if pets .get (id_ ) is None :
15
+ return connexion .NoContent , 404
16
+ return pets [id_ ]
17
+
18
+
19
+ def show ():
20
+ # NOTE: we need to wrap it with list for Python 3 as dict_values is not JSON serializable
21
+ return list (pets .values ())
22
+
23
+
24
+ app = connexion .FlaskApp (__name__ , specification_dir = "spec/" )
25
+ app .add_api ("openapi.yaml" , arguments = {"title" : "Pet Store Rel Ref Example" })
26
+ app .add_api ("swagger.yaml" , arguments = {"title" : "Pet Store Rel Ref Example" })
27
+
28
+
29
+ if __name__ == "__main__" :
30
+ app .run (f"{ Path (__file__ ).stem } :app" , port = 8080 )
Original file line number Diff line number Diff line change
1
+ components :
2
+ schemas :
3
+ Pet :
4
+ required :
5
+ - name
6
+ properties :
7
+ id :
8
+ type : integer
9
+ format : int64
10
+ readOnly : true
11
+ example : 1
12
+ name :
13
+ type : string
14
+ example : fluffy
15
+ registered :
16
+ type : string
17
+ readOnly : true
18
+ example : 2019-01-16T23:52:54Z
19
+
20
+ Pets :
21
+ type : array
22
+ items :
23
+ $ref : " #/components/schemas/Pet"
24
+
25
+ Error :
26
+ properties :
27
+ code :
28
+ type : integer
29
+ format : int32
30
+ message :
31
+ type : string
32
+ required :
33
+ - code
34
+ - message
Original file line number Diff line number Diff line change
1
+ definitions :
2
+ Pet :
3
+ type : object
4
+ required :
5
+ - name
6
+ properties :
7
+ id :
8
+ type : integer
9
+ format : int64
10
+ example : 1
11
+ name :
12
+ type : string
13
+ example : fluffy
14
+ registered :
15
+ type : string
16
+ format : date-time
17
+ example : 2019-01-16T23:52:54Z
18
+
19
+ Pets :
20
+ type : array
21
+ items :
22
+ $ref : " #/definitions/Pet"
23
+
24
+ Error :
25
+ type : object
26
+ properties :
27
+ code :
28
+ type : integer
29
+ format : int32
30
+ message :
31
+ type : string
32
+ required :
33
+ - code
34
+ - message
35
+
Original file line number Diff line number Diff line change
1
+ openapi : 3.0.0
2
+
3
+ info :
4
+ version : 1.0.0
5
+ title : Swagger Petstore
6
+ license :
7
+ name : MIT
8
+
9
+ servers :
10
+ - url : /openapi
11
+
12
+ paths :
13
+ /pets :
14
+ get :
15
+ summary : List all pets
16
+ operationId : app.show
17
+ responses :
18
+ ' 200 ' :
19
+ description : A list of pets
20
+ content :
21
+ application/json :
22
+ schema :
23
+ $ref : " components.yaml#/components/schemas/Pets"
24
+ default :
25
+ description : Unexpected error
26
+ content :
27
+ application/json :
28
+ schema :
29
+ $ref : " components.yaml#/components/schemas/Error"
30
+
31
+ ' /pets/{petId} ' :
32
+ get :
33
+ summary : Info for a specific pet
34
+ operationId : app.get
35
+ parameters :
36
+ - name : petId
37
+ in : path
38
+ description : Id of the pet to get.
39
+ required : true
40
+ schema :
41
+ type : integer
42
+ example : 1
43
+ responses :
44
+ ' 200 ' :
45
+ description : Expected response to a valid request
46
+ content :
47
+ application/json :
48
+ schema :
49
+ $ref : " components.yaml#/components/schemas/Pet"
50
+ default :
51
+ description : Unexpected error
52
+ content :
53
+ application/json :
54
+ schema :
55
+ $ref : " components.yaml#/components/schemas/Error"
Original file line number Diff line number Diff line change
1
+ swagger : " 2.0"
2
+
3
+ info :
4
+ title : " {{title}}"
5
+ version : " 1.0"
6
+
7
+ basePath : /swagger
8
+
9
+ paths :
10
+ /pets :
11
+ get :
12
+ summary : List all pets
13
+ operationId : app.show
14
+ responses :
15
+ ' 200 ' :
16
+ description : List all pets
17
+ schema :
18
+ type : array
19
+ items :
20
+ $ref : ' definitions.yaml#/definitions/Pets'
21
+ default :
22
+ description : Unexpected Error
23
+ schema :
24
+ $ref : ' definitions.yaml#/definitions/Error'
25
+
26
+ ' /pets/{petId} ' :
27
+ get :
28
+ summary : Info for a specific pet
29
+ operationId : app.get
30
+ parameters :
31
+ - name : petId
32
+ in : path
33
+ required : true
34
+ type : integer
35
+ minimum : 1
36
+ description : Parameter description in Markdown.
37
+ responses :
38
+ ' 200 ' :
39
+ description : Expected response to a valid request
40
+ schema :
41
+ $ref : ' definitions.yaml#/definitions/Pet'
42
+ default :
43
+ description : Unexpected Error
44
+ schema :
45
+ $ref : ' definitions.yaml#/definitions/Error'
You can’t perform that action at this time.
0 commit comments