Azure / autorest.go

Extension for AutoRest (https://github.com/Azure/autorest) that generates Go code
MIT License
69 stars 43 forks source link

How to Generate Constructors for Client Structs? #943

Closed suchitra-rudra closed 1 year ago

suchitra-rudra commented 1 year ago

Hi @jhendrixMSFT, @tadelesh

Cient ctors are generated with version 4.0.0-preview.44 but not with version 4.0.0-preview.47. How can I get autorest to generate ctors for client structs? I suppose this relates to the discussion in https://github.com/Azure/autorest.go/issues/867 and changes in https://github.com/Azure/autorest.go/pull/910

The swagger I am using has the following security definition:

  "securityDefinitions": {
    "azure_auth": {
      "type": "oauth2",
      "authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
      "flow": "implicit",
      "description": "Azure Active Directory OAuth2 Flow",
      "scopes": {
        "user_impersonation": "impersonate your user account"
      }
    }
  }

I have tried the switches --openapi-type=arm as well as --export-clients=true but neither seem to generate the ctor. Is the appropriate flag to use documented somewhere? I don't think I found anything here - https://github.com/Azure/autorest/blob/main/docs/generate/flags.md.

Command:

autorest --go --input-file=api/v3/swagger.json --output-folder=autorest-v4/v3/client --namespace=client --package-name=<package-name> --openapi-type=arm --verbose

Output:

AutoRest code generation utility [cli version: 3.6.3; node: v18.14.2]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
info    |    Loading AutoRest core      '/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist' (3.9.4)
verbose | [0.66 s] No configuration found at 'file:/go/src/xxx-rest-client/'.
verbose | [0.66 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/default-configuration.md'
verbose | [0.68 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/inspect.md'
verbose | [0.68 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/directives.md'
verbose | [0.69 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/pipeline.md'
verbose | [0.70 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/loader-openapi.md'
verbose | [0.71 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/miscellaneous.md'
verbose | [0.72 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-powershell.md'
verbose | [0.73 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-azureresourceschema.md'
verbose | [0.74 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-csharp.md'
verbose | [0.75 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-go.md'
verbose | [0.76 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-java.md'
verbose | [0.77 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-nodejs.md'
verbose | [0.79 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-php.md'
verbose | [0.80 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-python.md'
verbose | [0.81 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-ruby.md'
verbose | [0.82 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-typescript.md'
verbose | [0.83 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-validators.md'
verbose | [0.84 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-az.md'
verbose | [0.86 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-terraform.md'
verbose | [0.87 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-azure-functions.md'
verbose | [0.88 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/plugin-openapi-to-cadl.md'
verbose | [0.90 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/graphs.md'
verbose | [0.91 s]    Including configuration file 'file:/.autorest/@autorestcore@3.9.4/nodemodules/@autorest/core/dist/resources/help-configuration.md'
info    |    Loading AutoRest extension '@autorest/go' (~4.0.0-preview.42->4.0.0-preview.47)
verbose | [0.97 s]    Including extension configuration file 'file:/.autorest/@autorestgo@4.0.0-preview.47/nodemodules/@autorest/go/readme.md'
verbose | [0.97 s]    Including configuration file 'file:/.autorest/@autorestgo@4.0.0-preview.47/nodemodules/@autorest/go/readme.md'
verbose | [0.98 s]    Including configuration file 'file:/.autorest/@autorestgo@4.0.0-preview.47/nodemodules/@autorest/go/autorest-configuration.md'
info    |    Loading AutoRest extension '@autorest/modelerfour' (4.23.1->4.23.1)
verbose | [1.05 s]    Including extension configuration file 'file:/.autorest/@autorestmodelerfour@4.23.1/nodemodules/@autorest/modelerfour/readme.md'
verbose | [1.05 s]    Including configuration file 'file:/.autorest/@autorestmodelerfour@4.23.1/nodemodules/@autorest/modelerfour/readme.md'
verbose | [1.62 s] Reading OpenAPI 2.0 file file:/go/src/xxx-rest-client/api/v3/swagger.json
warning | PreCheck/CheckDuplicateSchemas | Checking for duplicate schemas, this could take a (long) while.  Run with --verbose for more detail.
verbose | [1.85 s] Found and removed 0 duplicate schema
warning | UsingTemporaryFlag | modelerfour.treat-type-object-as-anything options is a temporary flag. It WILL be removed in the future.
verbose | [1.99 s]   ModelerFour/api-version-mode:client
verbose | [1.99 s]   ModelerFour/api-version-parameter:constant
verbose | [1.99 s]   ModelerFour/use-model-namespace:false
info    | Autorest completed in 2.22s. 6 files generated.

Autoreset Version:

AutoRest code generation utility [cli version: 3.6.3; node: v18.14.2]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest

Showing All Installed Extensions

 Type       Extension Name                           Version      Location 
 core       @autorest/core                           3.9.4        /.autorest/@autorest_core@3.9.4
 extension  @autorest/go                             4.0.0-preview.47 /.autorest/@autorest_go@4.0.0-preview.47
 extension  @autorest/modelerfour                    4.23.1       /.autorest/@autorest_modelerfour@4.23.1
jhendrixMSFT commented 1 year ago

The switch you want is --azure-arm. Of course, this assumes that your service conforms to ARM specifics.

We don't generate client constructors for data-plane as there is no consistent pattern across data-plane services.

suchitra-rudra commented 1 year ago

Thank you for the quick response. --azure-arm also did not generate the constructor. Unfortunately, as consumers of swagger we don't have control over the specifics.

If I understand correctly, for swaggers that don't conform to ARM specifics, outside the generated code package we can't create a client instance with non-zero member values?

jhendrixMSFT commented 1 year ago

That's correct; by default, you have to hand-write client constructors. We do this in our Autorest test suite, here's an example.

suchitra-rudra commented 1 year ago

Okay thanks for the clarification. Closing this issue.