-
Notifications
You must be signed in to change notification settings - Fork 3
fix: Add type field for anyOf parameters #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -229,6 +229,8 @@ def get_swagger2_operation_parameters( | |||||
| schema_generator: GenerateJsonSchema, | ||||||
| model_name_map: ModelNameMap, | ||||||
| field_mapping: FieldMapping, | ||||||
| route: routing.APIRoute, | ||||||
| method: str, | ||||||
| ) -> List[Dict[str, Any]]: | ||||||
| parameters = [] | ||||||
| for param in all_route_params: | ||||||
|
|
@@ -251,7 +253,26 @@ def get_swagger2_operation_parameters( | |||||
| if field_info.in_.value == "body": | ||||||
| parameter["schema"] = schema | ||||||
| else: | ||||||
| parameter.update({k: v for (k, v) in schema.items() if k != "title"}) | ||||||
| # according to the https://swagger.io/specification/v2/#:~:text=If%20in%20is%20any%20value%20other%20than%20%22body%22%3A | ||||||
| # field "type" if required | ||||||
| parameter["type"] = schema.get("type", "string") # fallback | ||||||
| if "anyOf" in schema: | ||||||
| any_of = schema.pop("anyOf") | ||||||
| if {"type": "null"} in any_of: | ||||||
| parameter.update({"x-nullable": True, "required": False}) | ||||||
| any_of.remove({"type": "null"}) | ||||||
| if len(any_of) == 1: | ||||||
| parameter.update(any_of[0]) | ||||||
| else: | ||||||
| parameter.update({"type": "string"}) | ||||||
| logger.warning( | ||||||
| f"fastapi_swagger2: Unable to handle anyOf in parameters {any_of}, use string type. Route: {route.path_format}, Method: {method}, Param: {param.alias}" | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| parameter.update( | ||||||
| {k: v for (k, v) in schema.items() if k not in ["title", "anyOf"]} | ||||||
| ) | ||||||
| if field_info.description: | ||||||
| parameter["description"] = field_info.description | ||||||
| if field_info.example != Undefined: | ||||||
|
|
@@ -341,6 +362,8 @@ def get_swagger2_path( | |||||
| schema_generator=schema_generator, | ||||||
| model_name_map=model_name_map, | ||||||
| field_mapping=field_mapping, | ||||||
| route=route, | ||||||
| method=method, | ||||||
| ) | ||||||
| parameters.extend(operation_parameters) | ||||||
| if parameters: | ||||||
|
|
@@ -571,7 +594,7 @@ def get_swagger2( | |||||
| for p in properties: | ||||||
| if "anyOf" in properties[p].keys(): | ||||||
| any_of = properties[p].pop("anyOf") | ||||||
| if len(any_of) <= 2: | ||||||
| if len(any_of) <= 2 and {"type": "null"} in any_of: | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @luliangce I am little unsure on this condition change. Can you please share some sample code which would trigger this condition? Thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition is intended to handle optional properties ( Consider changing
Suggested change
|
||||||
| for _any_of in any_of: | ||||||
| if _any_of == {"type": "null"}: | ||||||
| properties[p]["x-nullable"] = True | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current logic for handling
anyOfin parameters has a flaw in the order of operations. It first determines the type fromanyOf, but then overwrites it with a more generic type from the baseschemadictionary. This can lead to incorrect parameter types in the generated Swagger specification (e.g., a parameter of typeOptional[int]being documented as astring).The logic should be reordered to first apply the general properties from the
schema, and then let the more specificanyOflogic override them. This ensures that the final parameter schema is correct.Consider replacing this block with the following implementation, which corrects the order of operations and also handles the case where
anyOfonly contains anulltype more gracefully.