-
Notifications
You must be signed in to change notification settings - Fork 1
Preserve tuple item types in Python SDKs #44
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 |
|---|---|---|
|
|
@@ -84,6 +84,11 @@ function literalValues(document: OpenApiDocument, input: JsonSchema, depth = 0): | |
| return values.every((value) => value !== undefined) ? values.flatMap((value) => value ?? []) : undefined; | ||
| } | ||
|
|
||
| function pythonArrayItemType(document: OpenApiDocument, schema: JsonSchema, collection = "list"): string { | ||
| const items = schema.items ? [schema.items] : (schema.prefixItems ?? []); | ||
| return [...new Set(items.map((item) => pythonType(document, item, collection)))].join(" | ") || "Any"; | ||
| } | ||
|
|
||
| function pythonType(document: OpenApiDocument, input: JsonSchema | undefined, collection = "list"): string { | ||
| const schema = resolveSchema(document, input); | ||
| if (!schema) return "Any"; | ||
|
|
@@ -115,7 +120,7 @@ function pythonType(document: OpenApiDocument, input: JsonSchema | undefined, co | |
| if (type === "integer") return result("int"); | ||
| if (type === "number") return result("float"); | ||
| if (type === "boolean") return result("bool"); | ||
| if (type === "array") return result(`${collection}[${pythonType(document, schema.items, collection)}]`); | ||
| if (type === "array") return result(`${collection}[${pythonArrayItemType(document, schema, collection)}]`); | ||
| if (type === "object" || schema.properties) return result("dict[str, Any]"); | ||
| return result("Any"); | ||
| } | ||
|
|
@@ -513,7 +518,10 @@ function modelSource(document: OpenApiDocument, resources: Map<string, PythonOpe | |
| return result(name); | ||
| } | ||
| const type = Array.isArray(schema.type) ? schema.type.find((item) => item !== "null") : schema.type; | ||
| if (type === "array") return result(`list[${modelType(schema.items, `${name}Item`)}]`); | ||
| if (type === "array") { | ||
| const itemType = schema.items ? modelType(schema.items, `${name}Item`) : pythonArrayItemType(document, schema); | ||
|
Member
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. π LOW: For a valid tuple such as |
||
| return result(`list[${itemType}]`); | ||
| } | ||
| if (type === "object" && typeof schema.additionalProperties === "object") { | ||
| return result(`dict[str, ${modelType(schema.additionalProperties, `${name}Value`)}]`); | ||
| } | ||
|
|
||
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.
π‘ MEDIUM: This treats
prefixItemsas the complete element domain, but JSON Schema allows additional unconstrained elements whenitemsis absent; the addedshapeschema therefore permits[1, 2, "x"]while generatinglist[float]. When both keywords are present, this branch also drops the prefix types by selecting onlyitems. Combine the prefix types with theitemstype, or includeAnyfor an unconstrained suffix unlessmaxItemsproves the tuple is bounded. turn0search3ξ