Customize @navigation to behave like @search for summary data re: metadata_fields

Sharing this because I wish plone.restapi did this out of the box, and I find it useful for my own purposes — you can customize @navigation service to behave like @search with regard to the additional metadata_fields function by making override of Navigation adapter and layer-specific override of the NavigationGet classes as follows:

from plone.restapi.serializer.summary import DefaultJSONSummarySerializer
from plone.restapi.services.navigation import get as _get


class Navigation(_get.Navigation):
    """
    Custom navigation adapter, customizes item metadata via
    customize_entry hook-point.
    """

    def customize_entry(self, item, brain):
        if 'metadata_fields' in self.request.form:
            # get summary serialization from plone.restapi summary serializer,
            # which is designed for search, but really should in ideal world
            # also work for navigation. Here we merge summary data when
            # 'metadata_fields' param is in the request querystring:
            # -- adapt catalog brain to summary metadata component: --
            summary = DefaultJSONSummarySerializer(brain, self.request)
            item.update(summary())  # merge summary k/v pairs into item dict


class NavigationGet(_get.NavigationGet):
    """
    Custom service to use custom Navigation implementation
    subclasses base from plone.restapi, implements customize_entry()
    method for purposes of metadata injection into resulting items
    in navigation JSON.
    """
    def reply(self):
        navigation = Navigation(self.context, self.request)
        return navigation(expand=True)["navigation"]