Closed mattdavpir closed 5 months ago
Hey @mattdavpir , hope you're great. You are right there were some changes in 0.18.x regarding default and optional fields during encoding.
You can configure the behaviour though:
SimpleRestJsonBuilder
.routes(service)
.withExplicitDefaultsEncoding(true)
.make
.pipe(ZIO.fromEither(_).orDie)
That should give you what you want.
The behaviour is indeed working as intended now: non-required fields are skipped during serialisation when they have the default value.
We ought to document the expected behaviour better, but I'm making the decision to close this issue.
That being said, thank you very much for the detailed issue, it is very much appreciated and I would love it if every issue opened by users was written with a similar level of attention.
Ah brilliant, thanks for the quick responses!
Hey, I'm not sure if this is intended behaviour or not, but it was unexpected from my end.
It seems that when I return a default value for a non-required field it is being omitted from the generated JSON, e.g. for this example:
MyService .smithy
build.sbt
Dependencies.scala
Main.scala
When I hit the endpoint the response is:
{"items":[{"stringField":"Hooray"},{},{"sortable":false,"stringField":"Boo"}]}
I was expecting the
"sortable": true
to be included in the serialized objects, which was the behaviour in 0.17.8 (maybe an intentional change in v0.18.0, but I couldn't find a mention of it in the change logs). If I set @required on the sortable field it will serialize the default properly.