loopback icon indicating copy to clipboard operation
loopback copied to clipboard

IDs are being generated for Model that has a base of "Model"

Open dashby3000 opened this issue 11 years ago • 9 comments

If I have a model defined as such:

{
  "name": "Foo",
  "base": "Model",
  "idInjection": false,
  "properties": {
    "bar": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

And it referenced as a type in a model defined like this:

{
  "name": "MyModel",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "sample": {
      "type": "Foo"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

In Swagger, it generates the following:

{
  "sample": {
    "bar": "",
    "id": 0
  },
  "id": 0
}

The id field from the model Foo should not be necessary since it is not a PersistedModel. The only way to get rid of it is to make one of the required properties of Foo and id field.

However, this is not a suitable workaround if none of the properties of Foo are required.

ref: PLATAPI-361

dashby3000 avatar Mar 17 '15 20:03 dashby3000

The following should work:

{
  "name": "BareModel",
  "idInjection": false,
  "base": "Model",
  "properties": {
    "id": false,
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

Please note that you need to set idInjection to false and hide the id property by setting its value to false or null, which prevents it from inheriting the property from the base model.

raymondfeng avatar Mar 26 '15 20:03 raymondfeng

@dashby3000 Please verify the solution.

raymondfeng avatar Mar 26 '15 21:03 raymondfeng

Verified. Thanks, @raymondfeng

dashby3000 avatar Apr 03 '15 21:04 dashby3000

@raymondfeng - Huh? Why is the id generated anyway, if idInjection: false? How does it make sense that I need to set two properties, in different scopes, to disable one feature? This isn't what I'd want, and doesn't match your documentation, see: http://docs.strongloop.com/display/public/LB/Model+definition+JSON+file#ModeldefinitionJSONfile-Top-levelproperties

shelbys avatar Apr 07 '15 01:04 shelbys

@raymondfeng - Does this require specific a specific version of loopback-datasource-juggler? I'm getting the following at startup if I set "id": false or "id": null:

    stack: Error: Invalid type for property id
        at Function.ModelClass.registerProperty (/Project/ApiGateway/node_modules/loopback-datasource-juggler/lib/model-builder.js:362:13)
        at ModelBuilder.defineClass [as define] (/Project/ApiGateway/node_modules/loopback-datasource-juggler/lib/model-builder.js:430:16)
        at Function.ModelClass.extend (/Project/ApiGateway/node_modules/loopback-datasource-juggler/lib/model-builder.js:343:33)
        at Function.registry.createModel (/Project/ApiGateway/node_modules/loopback/lib/registry.js:119:25)
        at /Project/ApiGateway/node_modules/loopback-boot/lib/executor.js:153:28
        at Array.forEach (native)
        at defineModels (/Project/ApiGateway/node_modules/loopback-boot/lib/executor.js:141:23)
        at setupModels (/Project/ApiGateway/node_modules/loopback-boot/lib/executor.js:130:3)
        at execute (/Project/ApiGateway/node_modules/loopback-boot/lib/executor.js:25:3)
        at bootLoopBackApp (/Project/ApiGateway/node_modules/loopback-boot/index.js:83:3)

shelbys avatar Apr 07 '15 01:04 shelbys

@raymondfeng - Confirmed, I had to upgrade loopback-datasource-juggler to 2.23.0 for the above to load

shelbys avatar Apr 07 '15 01:04 shelbys

I would just like to reiterate the question of shelbys: Why does setting idInjection to false also require explicitly setting id to false? And why is it not documented?

maritz avatar Jul 29 '15 12:07 maritz

It's a bit complicated. The base Model in loopback has an id property (which might not be the best choice). For a model extends from Model, it will inherit the id property regardless of idInjection.

Please note the exclusion of base properties is documented at http://docs.strongloop.com/display/LB/Model+definition+JSON+file#ModeldefinitionJSONfile-Excludepropertiesfrombasemodel

raymondfeng avatar Jul 29 '15 14:07 raymondfeng

Yes, but it's not obvious for someone reading the docs that you need both of these together because of the base Model. So the docs should maybe at least mention it in the idInjection section.

Maybe it shouldnt inherit the id from Model if idInjection is false though?!

maritz avatar Jul 30 '15 10:07 maritz