Skip to main content
added 233 characters in body
Source Link
marstato
  • 4.6k
  • 2
  • 17
  • 31

I'd go for something in between 1 and 2, for the reasons David mentioned:

You do not want to expose the ID of things unless necessary.

However, exposing the ID might become necessary at some point in time. If that happens, backwards compatibility is a concern. So, i would do this:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "name": "FEMALE"
    },
    "status": {
        "name": "FULL_TIME"
    }
}

That has the same properties as option 2 has; but it has the benefit that adding the ID later on does not introduce a BC break:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "id": 1,
        "name": "FEMALE"
    },
    "status": {
        "id": 3,
        "name": "FULL_TIME"
    }
}

As Eric points out in the comments, this still uses the entity name as a uniqe identifier. If the ID is introduced later, the name must still remain the same because older clients could (or rather will) have coded towards it.

I'd go for something in between 1 and 2, for the reasons David mentioned:

You do not want to expose the ID of things unless necessary.

However, exposing the ID might become necessary at some point in time. If that happens, backwards compatibility is a concern. So, i would do this:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "name": "FEMALE"
    },
    "status": {
        "name": "FULL_TIME"
    }
}

That has the same properties as option 2 has; but it has the benefit that adding the ID later on does not introduce a BC break:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "id": 1,
        "name": "FEMALE"
    },
    "status": {
        "id": 3,
        "name": "FULL_TIME"
    }
}

I'd go for something in between 1 and 2, for the reasons David mentioned:

You do not want to expose the ID of things unless necessary.

However, exposing the ID might become necessary at some point in time. If that happens, backwards compatibility is a concern. So, i would do this:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "name": "FEMALE"
    },
    "status": {
        "name": "FULL_TIME"
    }
}

That has the same properties as option 2 has; but it has the benefit that adding the ID later on does not introduce a BC break:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "id": 1,
        "name": "FEMALE"
    },
    "status": {
        "id": 3,
        "name": "FULL_TIME"
    }
}

As Eric points out in the comments, this still uses the entity name as a uniqe identifier. If the ID is introduced later, the name must still remain the same because older clients could (or rather will) have coded towards it.

Source Link
marstato
  • 4.6k
  • 2
  • 17
  • 31

I'd go for something in between 1 and 2, for the reasons David mentioned:

You do not want to expose the ID of things unless necessary.

However, exposing the ID might become necessary at some point in time. If that happens, backwards compatibility is a concern. So, i would do this:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "name": "FEMALE"
    },
    "status": {
        "name": "FULL_TIME"
    }
}

That has the same properties as option 2 has; but it has the benefit that adding the ID later on does not introduce a BC break:

{
    "id": 1,
    "firstName": "Jane",
    "lastName": "Doe",
    "active": true,
    "gender": {
        "id": 1,
        "name": "FEMALE"
    },
    "status": {
        "id": 3,
        "name": "FULL_TIME"
    }
}