Thank you Adam,

I sort of realized that at some point and then thought it is funny, laughing at myself and the way they wrote the documentation. The documentation explicitly states you have to download that specification and supply it, not that you have to create a specification based on that, I quote: 

1. Obtain the OpenAPI 2.0 specification for the REST service, in JSON format.

3. In the testing tool, create an HTTP request message as follows:

The above does state you downlaod the OpenAPI 2.0 specification in JSON format and supply that as the body in Postman, I then thought they will from that create a framework for your REST of some sort that you then fill out with your routes and calls, it is really obscure documentation, for somebody new to InterSystems and to swagger this is very ambiguous. laugh

My apologies, after typing all the above I realized what is happening (helps to explain to yourself), the SQLCOMPUTE increases the value in that field that is in the current record being saved and not what is in the last record that was saved. So you have to lock the record rather to not allow updates until you are done saving the record.

Apologies, the real definition of the property incudes %%INSERT, I removed it for the time being to check if that was the problem why it is not working, but the actual definition is:

/// Keep track of the version of the record each time it is updated, when newly created it starts at 1
Property InstanceVersion As %Integer [ SqlComputeCode = {set {*} = $INCREMENT({*}) }, SqlComputed, SqlComputeOnChange = (%%INSERT, %%UPDATE) ];
 

After some tests and trying to figure out what is going on I have to say it is not working. What I noticed now is that the SQLCompute works for both Insert and Update if the update comes from the Management Portal SQL or ObjectScript. The update however does not work for ODBC. 

In the FrontEnd the record is selcted via ODBC, the user makes the change to a field and it is saved via ODBC, what happens then is that the SQLCompute does not execute and the value that was in the field is overwritten with what was int he record coming from ODBC. The SQLCompute code thus does not execute on update if it comes from ODBC.

So here is the Property's definition:

/// Keep track of the version of the record each time it is updated, when newly created it starts at 1
Property InstanceVersion As %Integer [ SqlComputeCode = {set {*} = $INCREMENT({*}) }, SqlComputed, SqlComputeOnChange = %%UPDATE ];
 

I tested it:

Open the record in Front End via ODBC:

Then update from Management Portal. Before update:

After update the version increases:

then back to ODBC and make a change there and save:

The version remained 2, yet the Display value was updated via ODBC:

Update again from Management Portal:

Nice, version increased to 3

Now update again from ODBC:

Now the version has been set back to 2, URGGGH

Clearly this is not going to work, we shall then have to manually set the InstanceVersion from ODBC, which was not what I wanted to do.

I think I may have come closer to the problem, I have established that if the SqlComputeOnChange value is %%INSERT then the SqlComputeCode is executed ONLY when  a new record is created(inserted), and when it is %%UPDATE it is ONLY executing the SqlComputeCode  when a record is updated and NOT for insert as well. This is contradictory to what is stated in the InterSystems documentation, which states that the SqlComputeCode  for a value of  %%UPDATE is executed for BOTH insert and update, I quote:

"With %%UPDATE, Caché computes the field value when a row is inserted into the table and recomputes it when a row is updated. In both cases, Caché invokes the code specified in the SQLComputeCode keyword to set the value."

Now I am stuck because I need the property to be set on BOTH Insert AND Update and that seems not possible at all.
 

I think I may have possibly answered my own question, the FHIR Request Id shall only be filled out when it is supplied in the URL which is not the case for an update (PUT), but for a request such as GET where the id is part of the URL:

http://[base]/Patient/34567

where the Resource id is then 34567.

That implies if I need the Resource Id that is supplied in the JSON "id" field I have to first convert the request payload and de-serialize into the related FHIR resource class and get it then.

The JSON I am using:

{
  "resourceType" : "CodeSystem",
  "id" : "device-status",
  "meta" : {
    "lastUpdated" : "2019-11-01T09:29:23.356+11:00"
  },
  "text" : {
    "status" : "generated",
    "div" : "<div>!-- Snipped for Brevity --></div>"
  },
  "extension" : [{
    "url" : "http://hl7.org/fhir/StructureDefinition/structuredefinition-wg",
    "valueCode" : "oo"
  },
  {
    "url" : "http://hl7.org/fhir/StructureDefinition/structuredefinition-standards-st...",
    "valueCode" : "trial-use"
  },
  {
    "url" : "http://hl7.org/fhir/StructureDefinition/structuredefinition-fmm",
    "valueInteger" : 2
  }],
  "url" : "http://hl7.org/fhir/device-status-xxx",
  "identifier" : {
    "system" : "urn:ietf:rfc:3986",
    "value" : "urn:oid:2.16.840.1.113883.4.642.4.210"
  },
  "version" : "4.0.1",
  "name" : "FHIRDeviceStatus",
  "title" : "FHIRDeviceStatus",
  "status" : "draft",
  "experimental" : false,
  "date" : "2019-11-01T09:29:23+11:00",
  "publisher" : "HL7 (FHIR Project)",
  "contact" : [{
    "telecom" : [{
      "system" : "url",
      "value" : "http://hl7.org/fhir"
    },
    {
      "system" : "email",
      "value" : "fhir@lists.hl7.org"
    }]
  }],
  "description" : "The availability status of the device.",
  "caseSensitive" : true,
  "valueSet" : "http://hl7.org/fhir/ValueSet/device-status",
  "content" : "complete",
  "concept" : [{
    "code" : "active",
    "display" : "Active",
    "definition" : "The device is available for use.  Note: For *implanted devices*  this means that the device is implanted in the patient."
  },
  {
    "code" : "inactive",
    "display" : "Inactive",
    "definition" : "The device is no longer available for use (e.g. lost, expired, damaged).  Note: For *implanted devices*  this means that the device has been removed from the patient."
  },
  {
    "code" : "entered-in-error",
    "display" : "Entered in Error",
    "definition" : "The device was entered in error and voided."
  },
  {
    "code" : "unknown",
    "display" : "Unknown",
    "definition" : "The status of the device has not been determined."
  }]
}
 

Hi Paul,

Thank you for the response. You are correct, late at night and I did not notice that, I also trusted the JSON because I copied directly from FHIR CodeSystem, they have the JSON CodeSystem the identifier as a collection and yet the structure definition indicates singleton? That is definately the issue, I did not notice, thank goodness your eyes are fresh. I changed the JSON, now fine!

 I will try next time your suggestions for debugging.