Skip to content

Commit adce3a2

Browse files
committed
fix: update default value handling in array and object decorators
1 parent 35808f7 commit adce3a2

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

packages/chaingraph-types/src/decorator/array.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function PortArrayObject<T extends ObjectPortSchemaInput>(
125125
): PropertyDecorator {
126126
return PortArray({
127127
// Preset the item configuration for object elements with the provided schema.
128-
itemConfig: { type: 'object', schema, defaultValue: {} },
128+
itemConfig: { type: 'object', schema },
129129
...config,
130130
})
131131
}

packages/chaingraph-types/src/node/__test__/complex-node.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,19 @@ type UserStatusOptionId = keyof typeof userStatusOptions
100100
export class TestUserAddress {
101101
@PortString({
102102
description: 'Street of the address',
103+
defaultValue: '',
103104
})
104105
street?: string = ''
105106

106107
@PortString({
107108
description: 'City of the address',
109+
defaultValue: '',
108110
})
109111
city?: string = ''
110112

111113
@PortString({
112114
description: 'State of the address',
115+
defaultValue: '',
113116
})
114117
country?: string = 'EU'
115118

@@ -135,21 +138,25 @@ export class TestUserObject {
135138

136139
@PortString({
137140
description: 'Username of the user',
141+
defaultValue: '',
138142
})
139143
username: string = ''
140144

141145
@PortString({
142146
description: 'Name of the user',
147+
defaultValue: '',
143148
})
144149
name: string = ''
145150

146151
@PortNumber({
147152
description: 'Age of the user',
153+
defaultValue: 0,
148154
})
149155
age: number = 0
150156

151157
@PortNumber({
152158
description: 'Age of the user decimal',
159+
defaultValue: 0,
153160
})
154161
ageDecimal: number = 0
155162

@@ -167,6 +174,7 @@ export class TestUserObject {
167174
type: 'string',
168175
defaultValue: '',
169176
},
177+
defaultValue: [],
170178
})
171179
emails: string[] = []
172180

@@ -225,6 +233,7 @@ export class UserProfileNode extends BaseNode {
225233
// Case for infer schema from field value
226234
@Input() @PortObject({
227235
schema: TestUserObject,
236+
defaultValue: new TestUserObject(),
228237
})
229238
user1: TestUserObject = new TestUserObject()
230239

@@ -331,6 +340,7 @@ export class UserProfileNode extends BaseNode {
331340
defaultValue: new TestUserObject(),
332341
},
333342
},
343+
defaultValue: [],
334344
})
335345
users2DArray: TestUserObject[][] = []
336346

@@ -417,7 +427,9 @@ export class UserProfileNode extends BaseNode {
417427
numberArray?: number[]
418428

419429
@Output()
420-
@PortArrayObject(TestUserObject)
430+
@PortArrayObject(TestUserObject, {
431+
defaultValue: [],
432+
})
421433
simpleObjectArray?: TestUserObject[] = []
422434

423435
@Output()

packages/chaingraph-types/src/port/plugins/NumberPortPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export const NumberPortPlugin: IPortPlugin<'number'> = {
231231
)
232232
}
233233
}
234-
return data || undefined
234+
return data
235235
} catch (error) {
236236
throw new PortError(
237237
PortErrorType.SerializationError,

packages/chaingraph-types/src/port/plugins/ObjectPortPlugin.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export const ObjectPortPlugin: IPortPlugin<'object'> = {
378378

379379
deserializeValue: (data: JSONValue, config: ObjectPortConfig): ObjectPortValue => {
380380
if (data === undefined || data === null) {
381-
return {}
381+
return config.required ? {} : undefined
382382
}
383383
try {
384384
// Expecting an object with a "value" property that contains the serialized fields.
@@ -398,6 +398,14 @@ export const ObjectPortPlugin: IPortPlugin<'object'> = {
398398
for (const [key, fieldConfig] of Object.entries(config.schema.properties)) {
399399
const fieldSerializedValue = serializedFields[key]
400400
if (fieldSerializedValue === undefined) {
401+
const defaultVal = fieldConfig.defaultValue
402+
if (defaultVal !== undefined) {
403+
deserialized[key] = defaultVal
404+
} else if (fieldConfig.required) {
405+
// If the field is required but missing and has no default, set to undefined or handle as needed.
406+
deserialized[key] = undefined
407+
}
408+
401409
// If the field is required, you might want to throw an error.
402410
// For now, we simply continue to the next field.
403411
continue

0 commit comments

Comments
 (0)