Skip to content

Commit 475a678

Browse files
author
zach
authored
fix: respect required flag on properties (#19)
* fix: handle required and nullable better * fix: test
1 parent 14a4455 commit 475a678

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ function isOptional(type: String): boolean {
2121
return type.startsWith('Optional[')
2222
}
2323

24-
function toPythonTypeX(type: XtpNormalizedType): string {
24+
function toPythonTypeX(type: XtpNormalizedType, required: boolean = true): string {
2525
const opt = (t: string) => {
26-
return type.nullable ? `Optional[${t}]` : t
26+
return type.nullable || required === false ? `Optional[${t}]` : t
2727
}
2828
switch (type.kind) {
2929
case 'string':
@@ -64,12 +64,12 @@ function toPythonTypeX(type: XtpNormalizedType): string {
6464
}
6565

6666

67-
function toPythonType(property: XtpTyped): string {
68-
return toPythonTypeX(property.xtpType);
67+
function toPythonType(property: XtpTyped, required?: boolean): string {
68+
return toPythonTypeX(property.xtpType, required === true ? true : false);
6969
}
7070

71-
function toPythonParamType(property: XtpTyped): string {
72-
let t = toPythonTypeX(property.xtpType);
71+
function toPythonParamType(property: XtpTyped, required?: boolean): string {
72+
let t = toPythonTypeX(property.xtpType, required);
7373
// We need to represent bare int/float as a str in Python for now,
7474
// there may be some updates to the encoder we can make to handle
7575
// this case at some point

template/plugin/pdk_types.py.ejs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ class <%- pythonTypeName(schema.name) %>(Enum):
1818
@dataclass
1919
class <%- pythonTypeName(schema.name) %>(extism.Json):
2020
<% schema.properties.forEach(p => { -%>
21-
<% if (!p.nullable || p.required) {%>
21+
<% if (!p.nullable && p.required) {%>
2222
<% if (p.description) { -%>
2323
# <%- formatCommentBlock(p.description, "# ") %>
2424
<% } -%>
25-
<%- p.name %>: <%- toPythonType(p) %>
25+
<%- p.name %>: <%- toPythonType(p, p.required) %>
2626
<% } %>
2727
<% }) %>
2828
2929
<% schema.properties.forEach(p => { -%>
30-
<% if (p.nullable && !p.required) {%>
30+
<% if (p.nullable || !p.required) {%>
3131
<% if (p.description) { -%>
3232
# <%- formatCommentBlock(p.description, "# ") %>
3333
<% } -%>
34-
<%- p.name %>: <%- toPythonType(p) %> = None
34+
<%- p.name %>: <%- toPythonType(p, p.required) %> = None
3535
<% } %>
3636
<% }) %>
3737

tests/schemas/fruit.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ exports:
3939
contentType: application/json
4040
$ref: "#/components/schemas/ComplexObject"
4141
imports:
42+
eatAString:
43+
input:
44+
contentType: text/plain; charset=utf-8
45+
type: string
46+
output:
47+
contentType: text/plain; charset=utf-8
48+
type: string
49+
description: |
50+
This is a host function. Right now host functions can only be the type (i64) -> i64.
51+
We will support more in the future. Much of the same rules as exports apply.
4252
eatAFruit:
4353
input:
4454
contentType: text/plain; charset=utf-8
@@ -117,4 +127,13 @@ components:
117127
writeParams:
118128
"$ref": "#/components/schemas/WriteParams"
119129
nullable: true
130+
aMapOfNullableDateTimes:
131+
additionalProperties:
132+
type: string
133+
format: date-time
134+
nullable: true
135+
required:
136+
- aBoolean
137+
- aString
138+
- writeParams
120139
description: A complex json object

0 commit comments

Comments
 (0)