3

I couldn't find developers guide for a PUT call. What should we do if in a PUT request an optional value is passed as null. Do we set it to null in db or we retain the previous value in db if any? The issue with this approach is we won't be able to update the values to nullify optional fields once created with value.

Eg. For the same Entity suppose Student table, I have a nullable column of name suppose student_optional_subject. Now in POST student_optional_subject is set to value "calculus". Again a PUT request is made with student_optional_subject as null in the request body(i,e the field is missing in the request as it is an optional field).. shall I retain "calculus" in student_optional_subject or set it to null..

Arnav Karforma
  • 112
  • 2
  • 12
  • You need to provide more information. Show the http request code and input, how you're handing that input, and the db structure. – Jason Jul 31 '20 at 13:17
  • Well, I will explain my situation first... For a same Entity suppose Student table, I have a nullable column of name suppose student_optional_subject. Now in POST student_optional_subject is set to calculus. Again a PUT request is made with student_optional_subject as null in the request body.. shall I retain calculus in student_optional_subject or set it to null. – Arnav Karforma Jul 31 '20 at 13:44
  • by `put` means update everything coming from client. If you want to update only those limited values from client use `Patch` – bananas Jul 31 '20 at 14:51

2 Answers2

9

If a value in a PUT request is null, it should be saved as null. It should not be ignored because it's optional.

The way the PUT method is defined, it should always be a full update of the resource. If I do a PUT and then a GET of the same resource, the representation I receive from the GET should be equivalent to representation sent in the PUT.

If you ignore null fields, that's known as a partial update. PUT does not support partial updates. This is one of the most common things people get wrong when building REST APIs.

If you want to do partial updates, you can use the PATCH method. However, be careful because PATCH is used incorrectly even more often than PUT. If you use PATCH, you should be using a diff media type such as application/patch+json or application/merge-patch+json.

Community
  • 1
  • 1
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
-1

If you have a Student table, there should be no reason why the name of a student should be optional. If a Student exists, it should be understandable that they have a name associated with it. Each entry in the Student table could be associated with a primary-key like id that auto increments or uuid that is a randomly generated 64/128 bit unique id. At that point you should have another table called Subject. If the PUT doesn't contain a student_subject then you don't create a new Subject row. However, if it does, create a new StudentSubject for the given Student where the association is the id or uuid. This way, students can still have subjects or no subjects, but the Student table doesn't care if they do or not.

CREATE TABLE `student` (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(128),
    PRIMARY KEY (id)
);

CREATE TABLE `subject` (
    id int(11) NOT NULL AUTO_INCREMENT
    student_id int(11)
    title varchar(64)
    PRIMARY KEY (id)
);
Jason
  • 5,154
  • 2
  • 12
  • 22
  • Well, I will explain my situation first... For a same Entity suppose Student table, I have a nullable column of name suppose student_optional_subject. Now in POST student_optional_subject is set to calculus. Again a PUT request is made with student_optional_subject as null in the request body.. shall I retain calculus in student_optional_subject or set it to null. – Arnav Karforma Jul 31 '20 at 13:42
  • I edited my answer to clarify. – Jason Jul 31 '20 at 14:12
  • btw my actual tables are in required normalized form so I don't need to break them any further.. all the tables can have nullable and not null field.. my question is for the nullable field. – Arnav Karforma Jul 31 '20 at 14:19
  • Thats brutal, just use NULL then. If thats what you're limited to do then you kind of have to. – Jason Jul 31 '20 at 14:20
  • exactly, that's why I am confused, what is the standard developer recommendation for PUT APIs, as my APIs are public and don't have a limited set of consumer to get an agreement on values passed in the request body. So, I am trying to understand what is the standard process. – Arnav Karforma Jul 31 '20 at 14:26