Why does calling super in PostSerializer#raw cause a 500 error while calling object.raw works?

Hi everyone,

I’m learning how to extend Discourse serializers, and I ran into a behavior that I don’t fully understand.

I added this code to override PostSerializer#raw:

require_dependency "post_serializer"

class ::PostSerializer
  def raw
    if scope.can_edit?(object)
      object.raw
    else
      object.raw&.truncate(300)
    end
  end
end

This works perfectly.

However, if I change it to use super:

require_dependency "post_serializer"

class ::PostSerializer
  def raw
    if scope.can_edit?(object)
      super
    else
      object.raw&.truncate(300)
    end
  end
end

When scope.can_edit?(object) is true, the call to super causes /posts/<id>.json to return a 500 error.

I know I can avoid the problem by using object.raw instead of super, but I want to understand why super causes an exception here, especially because I also override other methods (cooked, post_stream, etc.) and super works fine in those cases.

Since I’m still new to Discourse internals, I would really appreciate an explanation of:

  • what super actually resolves to inside PostSerializer#raw,
  • why calling super in this case leads to a 500 error,
  • and why raw behaves differently compared to methods like cooked.

Thanks in advance for any help!

I’m pretty sure it’s because raw isn’t in the serializer for a normal post. It’s only when you edit that raw is included. To display a post you need only cooked; it would be a waste to also include raw when it’s not needed.

cooked and post stream are in the serializer, so super works.