biff pumpkin biff pumpkin - 4 months ago 22x
Android Question

Nested Models in Loopback Android SDK

I have a loopback instance with MongoDB backend, and have defined a model that has a nested (anonymous) model called 'location' as a property:

"name": "thing",
"plural": "things",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
"properties": {
"title": {
"type": "string",
"required": true
"description": {
"type": "string"
"location": {
"lat": {
"type": "string"
"lng": {
"type": "string"
"validations": [],
"relations": {},
"acls": [],
"methods": {}

In my android project, i'm using the loopback sdk to fetch all "thing" models into instance of a class that extends from loopback's model class.

ThingRepository repository = restAdapter.createRepository(Thing.class);
repository.findAll(/* callback code ommitted */)

public class Thing extends Model {
private String id;
private String title;
private String description;
private Location location;
/* getters/setters removed */

When I fetch all "things" from the server, and they all look fine except the nested "location" is always null. I've verified that I can fetch the Things from loopback's REST apis, with the location correctly populated. But it seems like the loopback client just wont deserialize and populate 'location'. "Location" is a simple java class with just 2 ints (lat and long). I've tried making it extend from 'Model' as well, but it still comes back as null.

public class Location extends Model {
private String lat;
private String lng;
/* getters/setters removed */

Any ideas?


This functionnality does not seem to be supported at the moment.

We should hopefully get this for free once we replace our hand-written JSON parser with a proper library supporting custom annotations

In the meantime, a no-brainer solution for you would be to create a location model, add a thing HasOne location relation, and use "api/thing/{id}/location" endpoint.

Or you can fork the project, change the JSON parsing library and make a pull request. It's up to you ;)

EDIT : Or, you've probably already thought of that but an even more simple solution, drop the nested object and use two classic properties location_lat location_lng