Skip to content

Serialization Bug: Including Null Properties in JSON Output instead of Excluding Them #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aniltonjunior opened this issue Jun 6, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@aniltonjunior
Copy link

aniltonjunior commented Jun 6, 2023

Describe the bug:
The serialization of models using the toArray and toJSON functions is including null properties instead of excluding them from the JSON string. These additional fields will cause an error when trying to instantiate the model with the saved JSON string.

Expected behavior:
I understand that using the object's properties is not the best approach. It is necessary to use only the information contained in the RawModelDataInput field, where the object's state is stored.

Schema:

"juros": {
	"$id": "#juros",
	"type": "object",
	"required": [
		"id"
	],
	"properties": {
		"id": {
			"type": ["integer", "string"],
			"description": "ID ou Sigla do tipo de indicador utilizado."
		},
		"dataInicioJuros": {
			"type": "string",
			"format": "datetime"
			"description": "Data de início de fluência dos juros."
		},
		"dataFinalJuros": {
			"type": "string",
			"format": "datetime"
			"description": "Data de término de fluência dos juros."
		},
		"jurosProRata": {
			"type": "boolean",
			"description": "Indica que o cálculo de juros considerará frações de períodos"
		},
		"jurosCompostos": {
			"type": "boolean",
			"description": "Indica que o cálculo de juros será da modalidade de juros compostos"
		}
	}
},

Version:
0.23.0

Additional context:
Considering the schema, I would like to create a model with the JSON string {"id": "JUROS_0"} and serialize it to the same output. However, the class will generate the following output JSON string: {"id": "JUROS_0", "dataInicioJuros": null, "dataFimJuros": null, "jurosProRata": null, "jurosCompostos": null}.

wol-soft pushed a commit to wol-soft/php-json-schema-model-generator-production that referenced this issue Jun 7, 2023
…lization result to avoid generating incompatible serialized values due to NULL fields (wol-soft/php-json-schema-model-generator#76)
@wol-soft wol-soft self-assigned this Jun 7, 2023
@wol-soft wol-soft added the bug Something isn't working label Jun 7, 2023
@wol-soft
Copy link
Owner

wol-soft commented Jun 7, 2023

Thanks for the report! Now properties which are optional and weren't provided in the initial data will be skipped in the serialization to generate a compatible serialized representation.
The properties will be included if they provide a default value or if the implicitNull option is enabled (compare e560d20#diff-cf7ae3135655cadc9420935644f596f479b07240ac019d479b872fc886b1eeb1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants