feat(api): sort openapi schemas

Reduces the constant changes to the frontend client types due to inconsistent ordering of pydantic models.
This commit is contained in:
psychedelicious 2024-05-29 21:00:02 +10:00
parent 5a4d10467b
commit 5beec8211a
2 changed files with 8 additions and 4 deletions

View File

@ -288,8 +288,9 @@ class AnyInvocation(BaseInvocation):
# Nodes are too powerful, we have to make our own OpenAPI schema manually # Nodes are too powerful, we have to make our own OpenAPI schema manually
# No but really, because the schema is dynamic depending on loaded nodes, we need to generate it manually # No but really, because the schema is dynamic depending on loaded nodes, we need to generate it manually
oneOf: list[dict[str, str]] = [] oneOf: list[dict[str, str]] = []
for i in BaseInvocation.get_invocations(): names = [i.__name__ for i in BaseInvocation.get_invocations()]
oneOf.append({"$ref": f"#/components/schemas/{i.__name__}"}) for name in sorted(names):
oneOf.append({"$ref": f"#/components/schemas/{name}"})
return {"oneOf": oneOf} return {"oneOf": oneOf}
@ -304,8 +305,9 @@ class AnyInvocationOutput(BaseInvocationOutput):
# No but really, because the schema is dynamic depending on loaded nodes, we need to generate it manually # No but really, because the schema is dynamic depending on loaded nodes, we need to generate it manually
oneOf: list[dict[str, str]] = [] oneOf: list[dict[str, str]] = []
for i in BaseInvocationOutput.get_outputs(): names = [i.__name__ for i in BaseInvocationOutput.get_outputs()]
oneOf.append({"$ref": f"#/components/schemas/{i.__name__}"}) for name in sorted(names):
oneOf.append({"$ref": f"#/components/schemas/{name}"})
return {"oneOf": oneOf} return {"oneOf": oneOf}

View File

@ -108,6 +108,8 @@ def get_openapi_func(
if post_transform is not None: if post_transform is not None:
openapi_schema = post_transform(openapi_schema) openapi_schema = post_transform(openapi_schema)
openapi_schema["components"]["schemas"] = dict(sorted(openapi_schema["components"]["schemas"].items()))
app.openapi_schema = openapi_schema app.openapi_schema = openapi_schema
return app.openapi_schema return app.openapi_schema