FastAPI 0.112.0 Duplicate operation_id Warning: Should the framework enforce uniqueness?
0 reputation · 11 Nov 2022, 23:12 UTC
0 reputation · 11 Nov 2022, 23:12 UTC
FastAPI 0.112.0 automatically generates operation_id values for each path operation using the function name and path. When two routers contain operations that resolve to the same operation_id, FastAPI logs a warning but still includes both entries in the generated OpenAPI schema.
This behavior presents a conflict for client‑generation tools that assume every operation_id is unique. The framework currently offers no mechanism to auto‑rename duplicates or to treat the conflict as a hard error, leaving developers to manually resolve the issue or rely on console warnings that may be suppressed in production.
The unresolved decision is whether FastAPI should:
operation_id values.Specifically:
operation_id values to maintain uniqueness while preserving the original intent?operation_id be preferable to avoid silent schema corruption, and how would this affect backward compatibility?operation_id values, such as OpenAPI Generator or Swagger UI?33275 reputation · 12 Nov 2022, 00:55 UTC
FastAPI should treat duplicate operation_id values as a hard error at startup rather than silently allowing them.
operation_id from the endpoint function name and the path.operation_id values.operation_id is unique; duplicates cause method name collisions or UI showing only the last endpoint.operation_id, so neither strategy changes latency or error rates.The warning‑only approach leaves the schema in an invalid state for consumers that rely on uniqueness. Because the identifier is only documentation metadata, making the conflict a hard error does not affect request processing but forces developers to resolve the clash before the application starts, preventing silent schema corruption.
def items(): under /items).operation_id./openapi.json and confirm that the two paths share the same operation_id value.operation_id arguments to the route decorators to make them unique; the warning disappears and the schema shows distinct IDs.If your project distributes generated SDKs or relies on stable operation_id values for external consumers, consider an auto‑rename strategy (e.g., appending a suffix) and bump the SDK version, because a hard error would break existing clients.
If the OpenAPI schema is used only for internal documentation or tooling that can be regenerated, enable the hard error. This can be done by setting app = FastAPI(..., generate_unique_id_function=custom_func) that raises on duplicates, or by awaiting a future FastAPI release that treats the warning as an error.
Do you publish or version SDKs generated from the OpenAPI spec for external consumers? Knowing this determines whether a hard error (preferred for internal use) or an auto‑rename with version bump (required for external SDKs) is the safer path.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.