athena-framework / athena

An ecosystem of reusable, independent components
https://athenaframework.org
MIT License
211 stars 17 forks source link

Better handle default case when returned object is not serializable #351

Closed Blacksmoke16 closed 8 months ago

Blacksmoke16 commented 8 months ago

Currently when you return a non-response object from a controller action, it is JSON serialized before being returned to the client. However, if the returned object is not a primitive type, and does not include JSON::Serializable or ASR::Serializable everything works the same, but the response body returned to the client is null. This is because the serializer component returns nil if it runs into something it is unable to serialize; which is a bit unexpected and a bit hard to debug if you do not know where to look.

Ideally this could be a compile time error, however the problem is that because of the https://athenaframework.org/Framework/View/FormatHandlerInterface/ feature, just because an object does not include those two types, doesn't necessarily mean the response body will be nil. It could be handled by a diff format, or someone could override the json format.

I'm thinking what we could do is if the data we want to serialize is not nil, the return type of the method is not Nil, and the serialize returns nil: Raise a runtime exception calling this out.

We could scope this to debug mode, but prob would be safe to have in both.