autorest icon indicating copy to clipboard operation
autorest copied to clipboard

C# code generation error when using global parameters with top level operations

Open natinimni opened this issue 8 years ago • 7 comments

AutoRest creates a non-compiling C# code when defining a swagger with global parameter and top\client level operation.

Consider the following swagger:

  "swagger": "2.0",
  "info": {
    "title": "FooBarClient",
    "version": "2016-09-01-Preview"
  },
  "x-ms-parameterized-host": {
    "parameters": [
      {
        "$ref": "#/parameters/SomeParameter"
      }
    ]
  },
  "consumes": ["application/json"],
  "produces": ["application/json"],
  "paths": {
    "/bar": {
      "get": {
        "operationId": "Bar",
        "responses": {"200": {"description": ""}}
      }
    },
    "/foo": {
      "get": {
        "operationId": "Foo_Get",
        "responses": {"200": {"description": ""}}
      }
    }
    
  },
  "parameters": {
    "SomeParameter": {
      "name": "someParameter",
      "in": "path",
      "required": true,
      "type": "string",
      "x-ms-parameter-location": "client"
    }
  }
}

While the generated C# code for "FooOperations" access 'SomeParameter' correctly using 'Client':

    internal partial class FooOperations : IServiceOperations<FooBarClient>, IFooOperations
    {
        public FooBarClient Client { get; private set; }
public async Task<AzureOperationResponse> GetWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (Client.SomeParameter == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SomeParameter");
            }
            . . .
        }
    }

But the generated "FooBarClient" code wrongly tries to access 'SomeParameter' in the same way, using 'Client', instead of accessing it using 'this':

    public partial class FooBarClient : ServiceClient<FooBarClient>, IFooBarClient, IAzureClient
    {
        public string SomeParameter { get; set; }

        public virtual IFooOperations Foo { get; private set; }

        public async Task<AzureOperationResponse> BarWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (this.Client.SomeParameter == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SomeParameter");
            }
            . . .
         }
    }

This is of course a compilation error. Furthermore, the generated code seems to be affected by the order of the operations in the swagger file - changing the order of 'foo' and 'bar' in the swagger, results in both FooBarClient and FooOperations accessing SomeParameter using 'this' (and not 'Client') - changing the compilation error to the FooOperations class.

Note that when defining only top-level operations, this works well. Am I doing something wrong here?

natinimni avatar Mar 26 '18 19:03 natinimni

O_o

It definitely looks like you've found a glitch here. I'm thinking it's related to x-ms-parameterized-host.

Worse, I know there are some weird bits in there that how something is used first, affected how it emitted code on subsequent use. Pretty much every time it has to say this.Client it's a ratsnest in those templates.

fearthecowboy avatar Mar 26 '18 19:03 fearthecowboy

Thanks @fearthecowboy , any ETA for a fix? this is currently blocking us (Azure.Search) from shipping a new feature.

Thanks, Nati

natinimni avatar Mar 26 '18 22:03 natinimni

I was just looking when I can look deeper into it.

I will try to find time to dig into this tomorrow; if it is terribly tricky, I may give you a short-term workaround fix you can stick in your configuration file.

G

fearthecowboy avatar Mar 26 '18 22:03 fearthecowboy

Thank you @fearthecowboy . Any updates?

natinimni avatar Mar 28 '18 18:03 natinimni

Replace the c# code generation settings with


``` yaml $(csharp)
csharp:
  azure-arm: true
  license-header: MICROSOFT_MIT_NO_VERSION
  namespace: Microsoft.Azure.Search
  clear-output-folder: true
  output-folder: $(csharp-sdks-folder)/Search/DataPlane/Microsoft.Azure.Search.Service/Generated

directive: 
  # fixes an incorrect generation in AutoRest with a parameterized host and both client and operation groups that use it.
  - from: source-file-csharp
    where: $
    transform: >
      if ( $.includes("class DataSourcesOperations") || $.includes("class IndexersOperations") || 
        $.includes("class IndexesOperations") ||  $.includes("class SynonymMapsOperations") ) 
        
        return $.
          replace(/this.SearchServiceName/g,"Client.SearchServiceName").
          replace(/this.SearchDnsSuffix/g,"Client.SearchDnsSuffix");
      return $;  
```

fearthecowboy avatar Mar 28 '18 23:03 fearthecowboy

Thanks!

natinimni avatar Mar 29 '18 17:03 natinimni

@fearthecowboy We've recently been burned by this issue when we added a new resource type to our SDK, but forgot to edit the above workaround in readme.md. This is a really unfortunate hack that we'd like to not have to maintain long term. Any chance a fix will be forthcoming soon?

FYI @arv100kri

bitsforthought avatar Feb 01 '19 03:02 bitsforthought