ID field with generated = true, forceId = true appears in Swagger example
Description/Steps to reproduce
I am using the following LoopBack model definition:
{
"name": "SetupDemo",
"description": "A transaction named SetupDemo",
"plural": "SetupDemo",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true,
"composer": {
"type": "transaction",
"namespace": "org.acme.biznet",
"name": "SetupDemo",
"fqn": "org.acme.biznet.SetupDemo"
}
},
"properties": {
"$class": {
"type": "string",
"default": "org.acme.biznet.SetupDemo",
"required": false,
"description": "The class identifier for this type"
},
"id": {
"type": "string",
"id": true,
"description": "The instance identifier for this type",
"required": false,
"generated": true
},
"timestamp": {
"type": "date",
"required": false
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": [],
"forceId": true
}
Each instance of this type has those three properties ($class, id, timestamp). The ID needs to be generated on the server side when creating new instances of this type. The ID must not be supplied by the client.
Expected result
The ID cannot be supplied by the client as I would expect:
The 'SetupDemo' instance is not valid. Details: 'id' can't be set (value: \"string\").
However the id property still appears in the generated Swagger example for the POST method:
Since the id property cannot be supplied in the POST method, it should not appear in the generated Swagger example.
Additional information
@sstone1 I can see that. Since swagger is mostly for development use, it shows id as you pointed out. This is by design. With forceId:true, POST from swagger will fail if id is specified with an error:
"error": {
"name": "ValidationError",
"status": 422,
"message": "The `CoffeeShop` instance is not valid. Details: `id` can't be set (value: 1234).",
...
With forceId:false, it will succeed with the manually supplied value, or auto-generated if it's not manually supplied.
One of the alternative implementations is to suppress id in example if forceId:true or present it if forceId:false as you suggest. I personally do not know the historical design decision, but I prefer the current implementation because in development phase, I think it's handy to manually set it and confirm it actually fails.
Please submit a new issue if needed. closing one month or older issues. thanks!
@Setogit
please provide a solution for this, the problem still persists. even after writing "transactionId" : false,
this is the error
{ "error": { "statusCode": 400, "name": "SyntaxError", "message": "Unexpected token t in JSON at position 110", "body": "{\n "$class": "test.AccountTransfer",\n "from": "HimaniAccount",\n "to": "AstatineAccount",\n "amount": 20,\n transactionId: false,\n "timestamp": "2018-06-01T04:33:39.539Z"\n}", "status": 400, "stack": "SyntaxError: Unexpected token t in JSON at position 110\n at JSON.parse ()\n at parse (/home/himani/.nvm/versions/node/v8.11.2/lib/node_modules/composer-rest-server/node_modules/body-parser/lib/types/json.js:88:17)\n at /home/himani/.nvm/versions/node/v8.11.2/lib/node_modules/composer-rest-server/node_modules/body-parser/lib/read.js:116:18\n at invokeCallback (/home/himani/.nvm/versions/node/v8.11.2/lib/node_modules/composer-rest-server/node_modules/raw-body/index.js:262:16)\n at done (/home/himani/.nvm/versions/node/v8.11.2/lib/node_modules/composer-rest-server/node_modules/raw-body/index.js:251:7)\n at IncomingMessage.onEnd (/home/himani/.nvm/versions/node/v8.11.2/lib/node_modules/composer-rest-server/node_modules/raw-body/index.js:307:7)\n at emitNone (events.js:106:13)\n at IncomingMessage.emit (events.js:208:7)\n at endReadableNT (_stream_readable.js:1064:12)\n at _combinedTickCallback (internal/process/next_tick.js:138:11)\n at process._tickCallback (internal/process/next_tick.js:180:9)" } }
writing true gives error 422