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..e6f24e7 100644 --- a/template/plugin/pdk_types.py.ejs +++ b/template/plugin/pdk_types.py.ejs @@ -18,20 +18,20 @@ 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, "# ") %> <% } -%> - <%- 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 <% } %> <% }) %> 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