Google Professional Cloud Developer Question 322
Single answerGoogle Cloud PlatformYou are developing a RESTful API that is widely used by client applications. A new requirement comes in to add a new field to the API response without breaking the existing behavior for current clients. How can you ensure backward compatibility while implementing this change?
- A
Add the new field to the response and document its usage in the API documentation.
- B
Replace an unused field in the response with the new field to avoid increasing response size.
- C
Add the new field to the response, but mark it as required in the API schema.
- D
Introduce a new version of the API to include the new field and deprecate the old version.
Show answer and explanation
Correct answer: A
Explanation
Backward compatibility ensures that existing clients can continue to function without modification when changes are made to an API. Adding a non-breaking change, such as a new field, to the API response while documenting it is the most appropriate way to maintain backward compatibility. Making the field optional ensures that older clients are unaffected.
- A. Correct.
This is the correct option. Adding the new field to the response and documenting it ensures that existing clients which do not expect the field will continue to function without issues, maintaining backward compatibility.
- B. Incorrect.
Replacing an existing field, even if unused, can potentially break clients that depend on the current API structure. This does not ensure backward compatibility.
- C. Incorrect.
Marking the new field as required would break existing clients that do not send or handle the new field, violating backward compatibility.
- D. Incorrect.
Introducing a new version of the API is a valid approach for changes that are not backward compatible, but it is unnecessary for adding a non-breaking new field. This adds unnecessary complexity.