Skip to content

Commit fabc549

Browse files
committed
Revert toFormValues to preserve Request DTO Type
1 parent c8c8d0b commit fabc549

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/use/metadata.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -324,25 +324,21 @@ export function makeDto(requestDto:string, obj?:any, ctx:{ createResponse?:() =>
324324
/** Mutates Request DTO values to supported HTML Input values */
325325
export function toFormValues(dto:any, metaType?:MetadataType|null) {
326326
if (!dto) return {}
327-
// avoid mutating reactive proxies; work on a shallow copy
328-
const out:any = Array.isArray(dto) ? Array.from(dto) : Object.assign({}, dto)
329-
Object.keys(out).forEach((key:string) => {
330-
const value = out[key]
331-
if (value instanceof Date) {
332-
out[key] = dateInputFormat(value)
333-
} else if (typeof value == 'string') {
327+
Object.keys(dto).forEach((key:string) => {
328+
let value = dto[key]
329+
if (typeof value == 'string') {
334330
if (value.startsWith('/Date'))
335-
out[key] = dateInputFormat(toDate(value))
331+
dto[key] = dateInputFormat(toDate(value))
336332
} else if (value != null && typeof value == 'object') {
337-
// shallow clone plain objects/arrays; leave Dates handled above
333+
// shallow clone
338334
if (Array.isArray(value)) {
339-
out[key] = Array.from(value)
335+
dto[key] = Array.from(value)
340336
} else {
341-
out[key] = Object.assign({}, value)
337+
dto[key] = Object.assign({}, value)
342338
}
343339
}
344340
})
345-
return out
341+
return dto
346342
}
347343

348344
/** Convert HTML Input values to supported DTO values */

tests/unit/Metadata.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ test('toFormValues', () => {
2121
const container = document.createElement('div')
2222
app.mount(container)
2323

24-
// toFormValues should not mutate and returns a shallow copy
25-
expect(result.a === result.b).toBe(false)
24+
// toFormValues mutates and returns original argument
25+
expect(result.a === result.b).toBe(true)
2626
expect(result.a.a).toBe('foo')
2727
expect(result.a.b).toBe(1)
2828
expect(result.a.c).toBe(null)

0 commit comments

Comments
 (0)