Forum Discussion

HenrikHL's avatar
HenrikHL
Frequent Contributor
2 years ago
Solved

Is there a way to reuse objects between POST and PUT `requests`?

Our request objects for POST and PUT are very similar - only an identifier (ID) differs in the two.

Is there a way to reuse an object between the two? I know that the readOnly property can be used for the response (and also for the GET) with regards to an ID field

 

Request object:
{
  name: foo
}
Response object:
{
  ID: 123
  name: foo
}

 

The problem with readOnly is that we cannot use it for the `PUT` request - it will not be rendered...

Only possibility to solve the above (as I see it) is to have 2 different objects (I know this can be done extending request-object with an `allOf`)...

If anyone has a "smart" design pattern for this I would be happy to hear about it 🙂

  • HenrikHL Hi

    You actually did summarize all the possibilities (2 distinct objects, allOf).
    I am not sure about that but if you define an object including all common properties, you can try to add "additionalProperties: true".
    In such case you will be able to use, several times, the object and to inject the ID only when you need.
    But in such way, the ID is taken into account only during the run time. 

2 Replies

  • chichepo's avatar
    chichepo
    Champion Level 3

    HenrikHL Hi

    You actually did summarize all the possibilities (2 distinct objects, allOf).
    I am not sure about that but if you define an object including all common properties, you can try to add "additionalProperties: true".
    In such case you will be able to use, several times, the object and to inject the ID only when you need.
    But in such way, the ID is taken into account only during the run time. 

    • HenrikHL's avatar
      HenrikHL
      Frequent Contributor

      Thank you chichepo 

      I was hoping there was a property called

      oncreate: false

      which would be applied on PUT but not on POST 😉 Maybe something for the future...