Skip to content

Trouble with Namespace Serialization #19

@th3morg

Description

@th3morg

Hi Tom! Sorry I fell off of the map for a while. I'm back and using HyperExpress-HAL 2.6. Some nice improvements have been made!

I have not used the namespace (curies) functionality before and am having some trouble getting the proper output.

public static void initializeHal(){
    HyperExpress.registerResourceFactoryStrategy(new HalResourceFactory(), PromoServicesConstants.PROMOTION_MEDIA_TYPE);
    HyperExpress
            .relationships()
            .addNamespaces(
                    new Namespace("docs", createAbsoluteURL("/docs#{rel}")).templated(true)
            )
            .forCollectionOf(Promotion.class)
                .rel("docs:GET", "promotions_get")
                .rel("docs:POST", "promotions_post")
                .rel(SELF, createAbsoluteURL(PROMOTIONS_BASE_PATH))
                .withQuery(getLinkQueryString(ACCOUNT_ID_QUERY_PARAM, ACCOUNT_ID_PATH_PARAM))
            .forClass(Promotion.class)
                .rel("docs:GET", "promotions__id__get")
                .rel(SELF, createAbsoluteURL(PROMOTION_BY_ID_PATH));
}

The generated resource looks like this (with null values removed):

{
    "_links": {
        "curies": [
        {
            "templated": false
        },
        {
            "templated": false
        }
        ],
        "docs:POST": {
            "href": "promotions_post"
        },
        "self": {
            "href": "http://localhost:8080/api/v1/promotions?accountId=someId"
        },
        "docs:GET": {
            "href": "promotions_get"
        }
    },
        "_embedded": {
        "promotions": [
        {
            "_links": {
            "self": {
                "href": "http://localhost:8080/api/v1/promotions/5733df547dce9f7d92d57984"
            },
            "docs:GET": {
                "href": "promotions__id__get"
            }
        },
            "id": "5733df547dce9f7d92d57984",
                "accountId": "someId"
        }
        ]
    }
}

You can see that the Namespace POJO fails to serialize properly. I traced the code path in debug mode and found that the POJO is seemingly constructed properly, but that the serialization step in the Jackson ObjectMapper doesn't work right for Namespace.

Tracing through everything, it looks like the Jackson BeanSerializer gets the "properties" of a POJO based on the Getters/Setters of the POJO unless explicitly annotated. Since the methods for getting name and href aren't "getX()" or "isX()" methods, it doesn't serialize them. This is why it does serialize "templated", which is an "isX()" method.

    //com.strategicgains.hyperexpress.domain.Namespace;
/**
 * Retrieve the namespace name.
 * 
 * @return the namespace name.
 */
public String name()
{
    return name;
}

/**
 * Retrieve the URL for the namespace.
 * 
 * @return the namespace href.
 */
public String href()
{
    return href;
}

I appreciate your insight. I can potentially work on a patch for this if you can get it released in maven central. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions