fastapi_swagger2 0.2.2__tar.gz → 0.2.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: fastapi_swagger2
3
- Version: 0.2.2
3
+ Version: 0.2.4
4
4
  Summary: Swagger2 support for FastAPI framework
5
5
  Project-URL: Homepage, https://github.com/virajkanwade/fastapi_swagger2
6
6
  Project-URL: Documentation, https://github.com/virajkanwade/fastapi_swagger2
@@ -94,7 +94,7 @@ Python 3.8+
94
94
 
95
95
  * 0.0.3 - FastAPI >= 0.79.0, <= 0.98.0
96
96
  * 0.1.1 - FastAPI >= 0.99.0, <= 0.99.1
97
- * 0.2.1 - FastAPI >= 0.100.0
97
+ * 0.2.4 - FastAPI >= 0.100.0
98
98
 
99
99
  ## Installation
100
100
 
@@ -133,6 +133,25 @@ This adds following endpoints:
133
133
  * http://localhost:8000/swagger2/docs
134
134
  * http://localhost:8000/swagger2/redoc
135
135
 
136
+ ## Generate spec for CI/CD
137
+
138
+ ```Python
139
+ import os
140
+
141
+ import yaml
142
+
143
+ from app.main import app
144
+
145
+ URL = os.environ["CLOUD_RUN_URL"]
146
+
147
+ app.servers.append(URL)
148
+
149
+ spec = app.swagger2()
150
+ spec['x-google-backend'] = {'address': URL}
151
+
152
+ print(yaml.dump(spec))
153
+ ```
154
+
136
155
  ## Development
137
156
 
138
157
  ```console
@@ -25,7 +25,7 @@ Python 3.8+
25
25
 
26
26
  * 0.0.3 - FastAPI >= 0.79.0, <= 0.98.0
27
27
  * 0.1.1 - FastAPI >= 0.99.0, <= 0.99.1
28
- * 0.2.1 - FastAPI >= 0.100.0
28
+ * 0.2.4 - FastAPI >= 0.100.0
29
29
 
30
30
  ## Installation
31
31
 
@@ -64,6 +64,25 @@ This adds following endpoints:
64
64
  * http://localhost:8000/swagger2/docs
65
65
  * http://localhost:8000/swagger2/redoc
66
66
 
67
+ ## Generate spec for CI/CD
68
+
69
+ ```Python
70
+ import os
71
+
72
+ import yaml
73
+
74
+ from app.main import app
75
+
76
+ URL = os.environ["CLOUD_RUN_URL"]
77
+
78
+ app.servers.append(URL)
79
+
80
+ spec = app.swagger2()
81
+ spec['x-google-backend'] = {'address': URL}
82
+
83
+ print(yaml.dump(spec))
84
+ ```
85
+
67
86
  ## Development
68
87
 
69
88
  ```console
@@ -0,0 +1,3 @@
1
+ [flake8]
2
+ max-line-length=88
3
+
@@ -1,4 +1,4 @@
1
- __version__ = "0.2.2"
1
+ __version__ = "0.2.4"
2
2
 
3
3
  from typing import Any, Dict, List, Optional, TypeVar
4
4
 
@@ -139,7 +139,7 @@ class FastAPISwagger2:
139
139
  contact=self.app.contact,
140
140
  license_info=self.app.license_info,
141
141
  routes=self.app.routes,
142
- tags=self.app.swagger2_tags,
142
+ tags=self.app.openapi_tags,
143
143
  )
144
144
 
145
145
  return self.app.swagger2_schema
@@ -327,7 +327,7 @@ class Swagger2(BaseModel):
327
327
  Dict[str, Union[ParameterBody, ParameterNotBody, Reference]]
328
328
  ] = None
329
329
  responses: Optional[Dict[str, Union[Response, Reference]]] = None
330
- securityDefinitions: Optional[Dict[str, Union[SecurityScheme, Reference]]]
330
+ securityDefinitions: Optional[Dict[str, Union[SecurityScheme, Reference]]] = None
331
331
  security: Optional[List[Dict[str, List[str]]]] = None
332
332
  tags: Optional[List[Tag]] = None
333
333
  externalDocs: Optional[ExternalDocumentation] = None
@@ -64,15 +64,17 @@ validation_error_response_definition = {
64
64
  },
65
65
  }
66
66
 
67
+ FieldMapping = Dict[
68
+ Tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue
69
+ ]
70
+
67
71
 
68
72
  # def get_schema_from_model_field(
69
73
  # *,
70
74
  # field: ModelField,
71
75
  # schema_generator: GenerateJsonSchema,
72
76
  # model_name_map: ModelNameMap,
73
- # field_mapping: Dict[
74
- # Tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue
75
- # ],
77
+ # field_mapping: FieldMapping,
76
78
  # ) -> Dict[str, Any]:
77
79
  # # This expects that GenerateJsonSchema was already used to generate the definitions
78
80
  # json_schema = field_mapping[(field, field.mode)]
@@ -110,6 +112,7 @@ def get_swagger2_security_definitions(
110
112
  security_definitions = {}
111
113
  operation_security = []
112
114
  for security_requirement in flat_dependant.security_requirements:
115
+ skip: bool = False
113
116
  # fastapi.security.* which gets model from fastapi.openapi.models
114
117
  security_definition = jsonable_encoder(
115
118
  security_requirement.security_scheme.model,
@@ -120,13 +123,13 @@ def get_swagger2_security_definitions(
120
123
  if security_definition.get("scheme", "basic") == "basic":
121
124
  security_definition = {"type": "basic"}
122
125
  else:
126
+ skip = True
123
127
  logger.warning(
124
128
  f"fastapi_swagger2: Unable to handle security_definition: {security_definition}"
125
129
  )
126
130
  elif security_definition["type"] == "apiKey":
127
131
  pass
128
132
  elif security_definition["type"] == "oauth2":
129
- _security_definition = security_definition
130
133
  flows = security_definition["flows"]
131
134
  flows_keys = list(flows.keys())
132
135
  if len(flows_keys) >= 1:
@@ -148,12 +151,14 @@ def get_swagger2_security_definitions(
148
151
  {_security_name: security_requirement.scopes}
149
152
  )
150
153
  else:
154
+ skip = True
151
155
  logger.warning(
152
156
  f"fastapi_swagger2: Unable to handle security_definition: {security_definition}"
153
157
  )
154
- security_name = security_requirement.security_scheme.scheme_name
155
- security_definitions[security_name] = security_definition
156
- operation_security.append({security_name: security_requirement.scopes})
158
+ if not skip:
159
+ security_name = security_requirement.security_scheme.scheme_name
160
+ security_definitions[security_name] = security_definition
161
+ operation_security.append({security_name: security_requirement.scopes})
157
162
  return security_definitions, operation_security
158
163
 
159
164
 
@@ -162,9 +167,7 @@ def get_swagger2_operation_parameters(
162
167
  all_route_params: Sequence[ModelField],
163
168
  schema_generator: GenerateJsonSchema,
164
169
  model_name_map: ModelNameMap,
165
- field_mapping: Dict[
166
- Tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue
167
- ],
170
+ field_mapping: FieldMapping,
168
171
  ) -> List[Dict[str, Any]]:
169
172
  parameters = []
170
173
  for param in all_route_params:
@@ -203,9 +206,7 @@ def get_swagger2_operation_request_body(
203
206
  body_field: Optional[ModelField],
204
207
  schema_generator: GenerateJsonSchema,
205
208
  model_name_map: ModelNameMap,
206
- field_mapping: Dict[
207
- Tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue
208
- ],
209
+ field_mapping: FieldMapping,
209
210
  ) -> Optional[Dict[str, Any]]:
210
211
  if not body_field:
211
212
  return None
@@ -239,9 +240,7 @@ def get_swagger2_path(
239
240
  operation_ids: Set[str],
240
241
  schema_generator: GenerateJsonSchema,
241
242
  model_name_map: ModelNameMap,
242
- field_mapping: Dict[
243
- Tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue
244
- ],
243
+ field_mapping: FieldMapping,
245
244
  ) -> Tuple[Dict[str, Any], Dict[str, Any], Dict[str, Any]]:
246
245
  path: Dict[str, Any] = {}
247
246
  security_schemes: Dict[str, Any] = {}
@@ -507,7 +506,7 @@ def get_swagger2(
507
506
  # output["definitions"] = {k: definitions[k] for k in sorted(definitions)}
508
507
  output["definitions"] = {}
509
508
  for k in sorted(definitions):
510
- properties = definitions[k]["properties"]
509
+ properties = definitions[k].get("properties", [])
511
510
  for p in properties:
512
511
  if "anyOf" in properties[p].keys():
513
512
  any_of = properties[p].pop("anyOf")
@@ -518,8 +517,9 @@ def get_swagger2(
518
517
  else:
519
518
  properties[p].update(_any_of)
520
519
  else:
520
+ properties[p].update({"type": "string"})
521
521
  logger.warning(
522
- f"fastapi_swagger2: Unable to handle anyOf in definitions {any_of}"
522
+ f"fastapi_swagger2: Unable to handle anyOf in definitions {any_of}, defaulting to string type."
523
523
  )
524
524
 
525
525
  output["definitions"][k] = definitions[k]