autorest icon indicating copy to clipboard operation
autorest copied to clipboard

There is no way to specify a mapping from REST API property names to client names

Open bitsforthought opened this issue 11 years ago • 13 comments

Sometimes the names we use in the REST API are not really ideal for the client libraries. Consider the case of Boolean properties. In .NET client libraries, the convention is to start Boolean property names with a prefix like "Is" or "Has". However, you wouldn't really want to clutter up a JSON REST API with these prefixes. For example, a property named "searchable" in the REST API ought to be named "IsSearchable" in the .NET client.

As another example, consider the OData V4 parameter $count. It's a Boolean parameter that indicates whether to include the total count of results of an OData query in each page of results. However, the name is not very suggestive. In a client library a more descriptive name like "IncludeTotalCount" might be more appropriate. In this case, the choice of name in the REST API is imposed by the OData protocol, so renaming it for the sake of a better client SDK experience is not an option (for a REST API with non-trivial production usage, renaming things is rarely a realistic option).

bitsforthought avatar Aug 27 '15 04:08 bitsforthought

I believe this is an opinion which could be taken by the .Net code generator. It certainly seems more idiomatic to approach naming booleans in this way.

devigned avatar Aug 28 '15 14:08 devigned

To recap our offline discussion: It's difficult in general to do the right thing for Boolean prefixes (sometimes you want "is", sometimes "has", sometimes "should", etc.). The most robust solution is to allow mapping source names to target names. Since Swagger doesn't support this today, this is not a quick fix. However, I would argue that this makes Swagger substantially less useful for mapping existing REST APIs (i.e. -- APIs that weren't designed with Swagger in mind) to a client programming model.

Can we at least make sure that workarounds exist in each of the target languages? For example, generated C# code uses partial classes so I can always omit properties from the Swagger spec that I want to rename, and rename them in the C# code. This isn't great, but it's better than making a bunch of pointless breaking changes to our client library.

bitsforthought avatar Sep 30 '15 22:09 bitsforthought

To add one more example of where re-naming properties would be useful: Consider a "List" operation like GET /indexes. We're required by OData to return JSON that looks like this:

{
    value: [
        { "name": "abc", ... },
        ...
    ]
}

But now we end up with a method that returns an object with a property named "Value" instead of something more semantically meaningful like "Indexes".

bitsforthought avatar Oct 05 '15 21:10 bitsforthought

One more real-world example: A part of our API is going to be a pass-through for part of another REST API that we don't control. It has property names that use underscores, max_length and word_list. We can't change this API, but we'd still like to be able to generate model classes for it and map the names to PascalCase/camelCase (depending on target language).

bitsforthought avatar Oct 07 '15 17:10 bitsforthought

Proposal to add "x-ms-client-name" extension

stankovski avatar Feb 19 '16 22:02 stankovski

@devigned @stankovski is there any update/ETA on this functionality being incorporated into the generator? Some of our customers are starting to complain about the large amount of refactoring they are needing to do from our existing Hydra clients to the new Swagger clients due to all the property renaming from our swagger specs since we can't define a friendly name.

begoldsm avatar Mar 14 '16 18:03 begoldsm

x-ms-client-name extension has been added to the master branch. Please take a look at the documentation over here: https://github.com/Azure/autorest/blob/master/Documentation/swagger-extensions.md#x-ms-client-name

amarzavery avatar Mar 14 '16 18:03 amarzavery

@begoldsm this was merged recently https://github.com/Azure/autorest/pull/801 and described here: https://github.com/Azure/autorest/blob/master/Documentation/swagger-extensions.md#x-ms-client-name. Does this solve the issue @begoldsm and @brjohnstmsft?

devigned avatar Mar 14 '16 18:03 devigned

Awesome thanks @amarzavery and @devigned! I am trying it out now and will let you know if it works.

begoldsm avatar Mar 14 '16 19:03 begoldsm

@devigned please change the milestone for this issue to reflect the release it will go into and close if this has already been implemented

stankovski avatar Mar 14 '16 19:03 stankovski

I won't have a chance to try it out for a while, but from the docs it looks like exactly what we need. Thanks!

bitsforthought avatar Mar 15 '16 00:03 bitsforthought

x-ms-client-name works as advertised. Closing this issue. Thanks!

bitsforthought avatar May 27 '16 01:05 bitsforthought

Agreed with @brjohnstmsft this is working and a great addition! Thank you very much!

begoldsm avatar May 27 '16 21:05 begoldsm