0

I have a method definition:

public async Task<IHttpActionResult> FindUsers([FromBody]User user)

This uses a class:

public class User
{
    public string UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I would like to extend the functionality of this without breaking the existing endpoint for current clients.

All clients make requests to the RESTful endpoint using an instance of System.Net.Http.HttpClient. They accept the JSON response and deserialize it into a list of User instances:

var user = JsonConvert.DeserializeObject<User>(content);

I would like to add a property to the User class:

public IList<string> Countries { get; set; }

I do not want this to break the endpoint for existing clients. That is, I want them to be able to continue deserializing into instances of their User class without problems.

I would also like them to be able to deserialize into an updated version of the User class if they wish to take advantage of the updated functionality.

The extended functionality would be implemented in the endpoint. It would detect if the Countries list has been provided in the request and, if so, perform a different operation and, thus, return a response - one which includes the Countries list.

Is this possible to do without breaking the endpoint for existing clients?

3
  • 1
    restfulapi.net/versioning Commented Sep 13, 2019 at 8:16
  • I Think what you need is to define the base clase User and then have User "Childs" class's that will have all the variations that you want. Commented Sep 13, 2019 at 8:23
  • 1
    If we skip all the "rules" about restful API:s I would say that as long as the client uses JsonConvert.DeserializeObject it isn't an issue. Even if you pass Countries as a member to an client that doesn't want it it will not break. However, you will give the client more data than he/she asked for. I'm only saying that you can return Countries to all your clients without breaking any code. Commented Sep 13, 2019 at 8:25

1 Answer 1

1

In my experience, adding data like this is generally not a problem.

If you send the data to the client, there is no need for them to expect or use it.
They may not have a property to deserialise countries into, but that shouldn't be a problem.
They don't see it, don't use it, etc.

The signature of your method is not changing (you are still basically expecting a User object) so everyone could still use the same endpoint. If they happen to supply countries, then they will be deserialised into your newer model. If they don't supply countries, then that property will not be set.

It is then up to you to decide what to do based on whether that data is provided.

As you might expect, this is a rough answer based on my own experience.
Your situation may be (is likely) to be more complicated than expressed by the question, but hopefully this provide some help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.