Skip to content

Trouble with Namespace Serialization #19

Open

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!

Activity

  1. tfredrich commented on May 12, 2016

    @tfredrich
    Member

    Hmm... let me look through the unit tests and add one for your use case. Thanks for your post.

  2. th3morg commented on May 12, 2016

    @th3morg
    Author

    Hi @tfredrich - I forked and made the modification and it worked as I expected. Let me know if you want me to make a pull request, or if you're just going to make the changes yourself. Thanks for getting into it so quickly!

  3. tfredrich commented on Oct 12, 2017

    @tfredrich
    Member

    I see what you're saying. You are likely using a "default" Jackson configuration, which I don't do (I use direct field access instead of the getters/setters). I'll take a stab at fixing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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