autorest icon indicating copy to clipboard operation
autorest copied to clipboard

[C#] Allow same HTTP verb in one operation (with one path)

Open MarkLie opened this issue 10 years ago • 3 comments

Hi community.

Currently it is not possible to specify something like this:

{
  "/contents/{contentId}": {
    "get": {
      "operationId": "getDocument",
      // ...
      "responses": {
        "200": {
            "description": "OK",
            "schema": {
                "$ref": "#/definitions/Document"
            }
        },
      }
    },
    "get": {
      "operationId": "getPost",
      // ...
      "responses": {
        "200": {
            "description": "OK",
            "schema": {
                "$ref": "#/definitions/Post"
            }
        },
      }
    }
  }  
}

(Please note, that I left some of the details in the above code snippet.)

From my point of view, it would be important to specify different operations (with different operationIds) having the same path. The above example is taken from the REST API of the Jive platform: https://developers.jivesoftware.com/api/v3/cloud/rest/ContentService.html#getContent(String,%20String,%20boolean,%20List%3CString%3E)

On the Jive platform, a content object could be a document, a post, etc. These content objects share some of their properties, which means from an object oriented perspective they all inherit from content object (they all are content objects).

The current behavior is that only one of the above operations would result in C# code being generated. I would like to strongly encourage you to change that.

Ciao, Mark

MarkLie avatar Jul 25 '16 15:07 MarkLie

Hi @MarkLie,

Thank you for the bring this up. Unfortunately, I don't think we are going to change the way this is handled.

This is forbidden in the 2.0 version of the Swagger specification http://swagger.io/specification/ and implied by the schema of the JSON.

If you would like to see something like this added in the future, I recommend petitioning the OpenAPI specification (the new version of the Swagger specification) by creating an issue there (https://github.com/OAI/OpenAPI-Specification/issues).

I don't think we will pursue multiple verb definitions until it is adopted by the OpenAPI standard.

devigned avatar Jul 25 '16 16:07 devigned

@MarkLie also, could you use a polymorphic discriminator to have one method that returns a common base type?

You may also want to look at our x-ms-paths extension. You can use two path definitions to enable this scenario.

devigned avatar Jul 25 '16 16:07 devigned

Hi @devigned,

thank you for your quick answer. I understand that you close this issue. I still have some open questions:

  1. The x-ms-paths approach does not seem to work, because all the path definitions for /contents/{contentId} share the same parameter.
  2. I do not understand the polymorphic discriminator approach. Even if I had a polymorphic discriminator field in the base class Content, it is too late to cast it to any derived class (Post, Document, ...). The derived class needs to be taken into account while deserializing.
  3. The generated C# code for each derived class would differ only in very few lines. It would actually be very easy to cover the situation if a type parameter T is used:

Instead of

// ...
await GetContentWithHttpMessagesAsync(contentId);
// ...

public async Task<HttpOperationResponse<Content>> GetContentWithHttpMessagesAsync(string contentId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
{
    // ...
    var _result = new HttpOperationResponse<Content>();

    // ...
    _result.Body = SafeJsonConvert.DeserializeObject<Content>(_responseContent, this.DeserializationSettings);

    // ...
    return _result;
}

Generate something like

// ...
await GetContentWithHttpMessagesAsync<Content>(contentId);
// ...

public async Task<HttpOperationResponse<T>> GetContentWithHttpMessagesAsync<T>(string contentId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
{
    // ...
    var _result = new HttpOperationResponse<T>();

    // ...
    _result.Body = SafeJsonConvert.DeserializeObject<T>(_responseContent, this.DeserializationSettings);

    // ...
    return _result;
}

And then later use it like this in hand written code

await GetContentWithHttpMessagesAsync<Post>(contentId);
await GetContentWithHttpMessagesAsync<Document>(contentId);

MarkLie avatar Jul 26 '16 10:07 MarkLie