Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"metadata": {
"anyOf": [{ "type": "object", "properties": { "source": { "type": "string" } } }, { "type": "null" }]
},
"shape": { "type": "array", "prefixItems": [{ "type": "number" }, { "type": "number" }] },
"opaque": { "anyOf": [{}, { "type": "null" }] },
"exclusiveOpaque": { "oneOf": [{}, { "type": "string" }], "anyOf": [{}] }
}
Expand Down
1 change: 1 addition & 0 deletions lib/generators/python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ describe("Python generator", () => {
expect(types).toContain('"widgetId": str');
expect(types).toContain('"displayName": str | None');
expect(types).toContain('"metadata": NotRequired[WidgetsRetrieveResponseValueMetadata | None]');
expect(types).toContain('"shape": NotRequired[list[float]]');
expect(types).toContain('"opaque": NotRequired[Any]');
expect(types).toContain('"exclusiveOpaque": NotRequired[Any | str | None]');
expect(types).toContain("WidgetsRetrieveResponse = WidgetsRetrieveResponseValue | None");
Expand Down
12 changes: 10 additions & 2 deletions lib/generators/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’‘ MEDIUM: This treats prefixItems as the complete element domain, but JSON Schema allows additional unconstrained elements when items is absent; the added shape schema therefore permits [1, 2, "x"] while generating list[float]. When both keywords are present, this branch also drops the prefix types by selecting only items. Combine the prefix types with the items type, or include Any for an unconstrained suffix unless maxItems proves the tuple is bounded. turn0search3

}

function pythonType(document: OpenApiDocument, input: JsonSchema | undefined, collection = "list"): string {
const schema = resolveSchema(document, input);
if (!schema) return "Any";
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ“ LOW: For a valid tuple such as type: array, prefixItems: [{ "$ref": "#/components/schemas/Foo" }], this routes the item through pythonArrayItemType and then pythonType, which resolves object schemas to dict[str, Any]. Unlike the existing items path, it never calls modelType/addModel, so the generated SDK loses the referenced Foo TypedDict. Keep prefix-item model references on the model-aware path when generating response models.

return result(`list[${itemType}]`);
}
if (type === "object" && typeof schema.additionalProperties === "object") {
return result(`dict[str, ${modelType(schema.additionalProperties, `${name}Value`)}]`);
}
Expand Down