error while resolving nested objects
its-dibo opened this issue · 2 comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.24.0
Plugin version
No response
Node.js version
18.x
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
22
Description
I have the following schema
schema
{
"response": {
"200": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"created_at": {
"type": "string"
},
"modified_at": {
"type": "string"
},
"title": {
"type": "string",
"example": "unit #323",
"description": "the org unit's title"
},
"parent": {
"type": "string",
"description": "the parent org unit",
"nullable": true,
"example": null
},
"manager": {
"type": "object",
"properties": {
"code": {
"type": "string",
"example": "code#538",
"description": "employer code"
},
"name": {
"type": "string",
"example": "John"
},
"status": {
"type": "string",
"nullable": true,
"enum": [
"active",
"inactive"
]
},
"org_unit": {
"type": "string",
"nullable": true
}
}
}
}
}
},
"next": {
"type": "string"
},
"prev": {
"type": "string"
}
}
}
},
"querystring": {
"filter": {
"type": "string",
"description": "a json decoded string of fields to be filtered, or a sql where statement with query.values provided",
"example": {
"title": "value",
"parent": "value",
"manager": "value"
}
}
}
}
to summarize the schema, it is something like:
{
id,
title,
manager: {id, name, code, ....}
}
and the response is similar to
[
{id, manager: {id, name, ...}
]
but I got the following error
Cannot read properties of null (reading 'name')\n at anonymous3 (eval at build (node_modules/fast-json-stringify/index.js:175:23),
this error disappears when I remove the property manager from the schema
Steps to Reproduce
.
Expected Behavior
No response
Thanks for reporting!
Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.
This test works for me. @its-dibo feel free to reopen the issue or create a new one if you have a reproduce. Make sure that you don't send a null
as a manager
value. If you want to accept null
there you can use a null
type or nullable
keyword.
'use strict'
const { test } = require('tap')
const build = require('..')
test('issue 655', (t) => {
const schema = {
type: 'object',
properties: {
data: {
type: 'array',
items: {
type: 'object',
properties: {
id: {
type: 'string'
},
created_at: {
type: 'string'
},
modified_at: {
type: 'string'
},
title: {
type: 'string',
example: 'unit #323',
description: "the org unit's title"
},
parent: {
type: 'string',
description: 'the parent org unit',
nullable: true,
example: null
},
manager: {
type: 'object',
properties: {
code: {
type: 'string',
example: 'code#538',
description: 'employer code'
},
name: {
type: 'string',
example: 'John'
},
status: {
type: 'string',
nullable: true,
enum: [
'active',
'inactive'
]
},
org_unit: {
type: 'string',
nullable: true
}
}
}
}
}
},
next: {
type: 'string'
},
prev: {
type: 'string'
}
}
}
const stringify = build(schema)
const data = {
data: [
{
id: 'string',
created_at: 'string',
modified_at: 'string',
title: 'string',
parent: 'string',
manager: {
code: 'string',
name: 'string',
status: 'string',
org_unit: 'string'
}
}
],
next: 'string',
prev: 'string'
}
const res = stringify(data)
console.log(res)
t.end()
})