Search code examples
javagoogle-cloud-platformgoogle-cloud-endpoints

Google cloud endpoints. Omitting properties dynamically - by api method


The requirement is very simple. Let's say I have a model Book, with the properties:

  • author (string)
  • views (integer)
  • code (string)

Let's say I have code property, which is a String. I would like to keep this code property protected and not expose it in most of my API Methods. However, only in 2 API Methods, I would like to return the books, with the code property viewable.

The question in short is. How can you achieve this in google cloud endpoints (java), where I would like to omit specific properties from model in response, conditionally (According to API Method being called).

Anything that points me in the direction would be appreciated. For some reason I'm just unable to find this information in google docs, nor searching in google.

I thought of few solutions, but none elegant enough. For example, I could do kind of a transformer, which loops over models returned and set properties I wanna hide to null for example, but I feel like there's a more elegant solution that I'm missing.

Thank you in advance.


Solution

  • Here are a few options, but they might not be elegant enough:

    • Have one class per subset of properties (bonus: statically enforces that the properties won't appear)
    • Set properties you don't want to expose to null in each API

    I don't think transformers work for you, as they are API-wide.