From 85c96d3f24a247ce7f5eb07271b54b346024ed5c Mon Sep 17 00:00:00 2001 From: zach Date: Thu, 19 Dec 2024 08:49:38 -0800 Subject: [PATCH 1/2] fix: handle required and nullable better --- src/index.ts | 12 ++++++------ template/plugin/pdk_types.py.ejs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9574356..19c6376 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,9 +21,9 @@ function isOptional(type: String): boolean { return type.startsWith('Optional[') } -function toPythonTypeX(type: XtpNormalizedType): string { +function toPythonTypeX(type: XtpNormalizedType, required: boolean = true): string { const opt = (t: string) => { - return type.nullable ? `Optional[${t}]` : t + return type.nullable || required === false ? `Optional[${t}]` : t } switch (type.kind) { case 'string': @@ -64,12 +64,12 @@ function toPythonTypeX(type: XtpNormalizedType): string { } -function toPythonType(property: XtpTyped): string { - return toPythonTypeX(property.xtpType); +function toPythonType(property: XtpTyped, required?: boolean): string { + return toPythonTypeX(property.xtpType, required === true ? true : false); } -function toPythonParamType(property: XtpTyped): string { - let t = toPythonTypeX(property.xtpType); +function toPythonParamType(property: XtpTyped, required?: boolean): string { + let t = toPythonTypeX(property.xtpType, required); // We need to represent bare int/float as a str in Python for now, // there may be some updates to the encoder we can make to handle // this case at some point diff --git a/template/plugin/pdk_types.py.ejs b/template/plugin/pdk_types.py.ejs index 5054b1e..a0aada6 100644 --- a/template/plugin/pdk_types.py.ejs +++ b/template/plugin/pdk_types.py.ejs @@ -22,16 +22,16 @@ class <%- pythonTypeName(schema.name) %>(extism.Json): <% if (p.description) { -%> # <%- formatCommentBlock(p.description, "# ") %> <% } -%> - <%- p.name %>: <%- toPythonType(p) %> + <%- p.name %>: <%- toPythonType(p, p.required) %> <% } %> <% }) %> <% schema.properties.forEach(p => { -%> -<% if (p.nullable && !p.required) {%> +<% if (p.nullable || !p.required) {%> <% if (p.description) { -%> # <%- formatCommentBlock(p.description, "# ") %> <% } -%> - <%- p.name %>: <%- toPythonType(p) %> = None + <%- p.name %>: <%- toPythonType(p, p.required) %> = None <% } %> <% }) %> From cb097996b9ed5f677ad1749ffa6e95cd0564c746 Mon Sep 17 00:00:00 2001 From: zach Date: Thu, 19 Dec 2024 10:28:06 -0800 Subject: [PATCH 2/2] fix: test --- template/plugin/pdk_types.py.ejs | 2 +- tests/schemas/fruit.yaml | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/template/plugin/pdk_types.py.ejs b/template/plugin/pdk_types.py.ejs index a0aada6..e6f24e7 100644 --- a/template/plugin/pdk_types.py.ejs +++ b/template/plugin/pdk_types.py.ejs @@ -18,7 +18,7 @@ class <%- pythonTypeName(schema.name) %>(Enum): @dataclass class <%- pythonTypeName(schema.name) %>(extism.Json): <% schema.properties.forEach(p => { -%> -<% if (!p.nullable || p.required) {%> +<% if (!p.nullable && p.required) {%> <% if (p.description) { -%> # <%- formatCommentBlock(p.description, "# ") %> <% } -%> diff --git a/tests/schemas/fruit.yaml b/tests/schemas/fruit.yaml index bdf0d1f..4ba0c62 100644 --- a/tests/schemas/fruit.yaml +++ b/tests/schemas/fruit.yaml @@ -39,6 +39,16 @@ exports: contentType: application/json $ref: "#/components/schemas/ComplexObject" imports: + eatAString: + input: + contentType: text/plain; charset=utf-8 + type: string + output: + contentType: text/plain; charset=utf-8 + type: string + description: | + This is a host function. Right now host functions can only be the type (i64) -> i64. + We will support more in the future. Much of the same rules as exports apply. eatAFruit: input: contentType: text/plain; charset=utf-8 @@ -117,4 +127,13 @@ components: writeParams: "$ref": "#/components/schemas/WriteParams" nullable: true + aMapOfNullableDateTimes: + additionalProperties: + type: string + format: date-time + nullable: true + required: + - aBoolean + - aString + - writeParams description: A complex json object