Azure / autorest.powershell

AutoRest PowerShell Generator
MIT License
112 stars 78 forks source link

[THREAD] PowerShell Generator - Quick Questions #153

Open fearthecowboy opened 5 years ago

fearthecowboy commented 5 years ago

If you have some quick questions about the PowerShell Generator, ask 'em here.

If you have an actual bug to report, go ahead and file an issue.

ghost commented 4 years ago

I am trying to set this up in a CI scenario using Azure Pipelines to generate, test, and pack the module to then get published to the PowerShell Gallery. Do yall have any examples or recommended suggestions for this?

Kurlee commented 4 years ago

I need a quick sanity check for my attempt to generate a powershell module for the VMWare Workstation Rest API.

Swagger file

Autorest command: autorest --powershell --input-file:.\vmware.json --output-folder:.\OutDir --namespace:My.Example.Namespace

The VMWare swagger file has a misspelling (can you believe it?), and besides that, everything is working for the most part. I have attempted to generate a CSharp client, but a fatal error occurs due to the 'produces' value. PowerShell module generation and build completes successfully, however, there is no way to pass a URI to the command. Notice, there is no host field in the swagger file, and I think, that may be the issue. The only flags in my generated cmdlets are the proxy, pipeline related.

Every command fails with : Invalid URI: The format of the URI could not be determined

Looking for a sanity check to confirm that I am not doing anything stupid.

ghost commented 4 years ago

Is there a way to leverage the values within the enum property of the swagger spec to populate a parameters ValidateSet options?

mockjv commented 3 years ago

@James-Allerton I'm also seeing this with the swagger v2 JSON exposed by TeamCity and I've tried to modify the JSON and cannot figure out what it doesn't like. Did you ever find a resolution to this?

JustinGrote commented 3 years ago

@fearthecowboy may be time to enable Github Discussions to replace this issue thread? https://docs.github.com/en/discussions

kjacobsen commented 3 years ago

Hey folks,

Getting some schema violation errors, is there any method to ignore these?

kjacobsen@Orbiting-Dragon temp % autorest --input-file=https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml --powershell
AutoRest code generation utility [cli version: 3.2.1; node: v12.22.1, max-memory: 2048 MB]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
   Loading AutoRest core      '/Users/kjacobsen/.autorest/@autorest_core@3.4.3/node_modules/@autorest/core/dist' (3.4.3)
INFORMATION:
The default version of @autorest/powershell has been bumped from 2.1+ to 3.0+.
 > If you still want to use 2.1+ version, please specify it with --use:@autorest/powershell@2.1.{x}, e.g 2.1.401.

INFORMATION: > Loading AutoRest extension '@autorest/powershell' (~3.0.0->3.0.430)
INFORMATION: > Loading AutoRest extension '@autorest/modelerfour' (4.15.414->4.15.414)
Process() cancelled due to failure
ERROR: Schema violation: should be array (paths > /companies/info > get > responses > 200 > schema > properties > authentications > items > properties > init_url > required)
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:317:12 ($)
ERROR: Schema violation: should be array (paths > /companies/info > get > responses > 200 > schema > properties > authentications > items)
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:304:9 ($)
ERROR: Schema violation: should match some schema in anyOf (paths > /companies/info > get > responses > 200 > schema > properties > authentications > items)
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:304:9 ($)
ERROR: Schema violation: should NOT have additional properties (paths > /companies/info > get > responses > 200 > schema)
  additionalProperty: properties
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:283:6 ($)
ERROR: Schema violation: should match exactly one schema in oneOf (paths > /companies/info > get > responses > 200 > schema)
  passingSchemas: null
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:283:6 ($)
ERROR: Schema violation: should be equal to one of the allowed values (paths > /companies/info > get > responses > 200 > schema > type)
  allowedValues: file
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:284:7 ($)
ERROR: Schema violation: should be array (paths > /companies/info > get > responses > 200 > schema > properties > authentications > items > properties > init_url > required)
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:317:12 ($)
ERROR: Schema violation: should be array (paths > /companies/info > get > responses > 200 > schema > properties > authentications > items)
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:304:9 ($)
ERROR: Schema violation: should match some schema in anyOf (paths > /companies/info > get > responses > 200 > schema > properties > authentications > items)
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:304:9 ($)
ERROR: Schema violation: should NOT have additional properties (paths > /companies/info > get > responses > 200 > schema)
  additionalProperty: properties
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:283:6 ($)
ERROR: Schema violation: should match exactly one schema in oneOf (paths > /companies/info > get > responses > 200 > schema)
  passingSchemas: null
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:283:6 ($)
ERROR: Schema violation: should be equal to one of the allowed values (paths > /companies/info > get > responses > 200 > schema > type)
  allowedValues: file
    - https://api.stoplight.io/v1/versions/BG4PXY5ZvdxL5AmYX/export/oas.yaml:284:7 ($)
VencislavKolev commented 3 years ago

Hi guys, I'm using autorest to generate powershell cmdlets from two (2) swagger definitions which share the same host name, but different base paths and some of the endpoints in both API specs are identitical example: host: mypage.com file1 base path: my/api file2 base path: my/saas/api endpoint from file1 and file2: users/{userId}/tasks/{taskId} endpoint from file1 and file2: users/{userId}/tasks/

I'm facing the following issues - generated c# cmdlets have the following names with endpoint for file 1 -Get-Task_Get / Get-Task_List with endpoint for file 2 - Get-Task_Get1 / Get-Task_List1 I had to go through the generated code and write down which .cs file correlates to which base path, however this isn't my biggest concern. The generated script has name of Get-Task.ps1, below are some of the possible input parameters. Now if I want to execute lets say users/{userId}/tasks/ for base path my/saas/api I have comment out Get, Get1 and List1 from first parameter so it calls the desired endpoint. Whole point is I have to play around to comment and uncomment parameters based on what I want. I would like to ask if there is a configuration I can write in my readme.md config file before running the autorest command or the only solution is to manually create separate ps1 with whats neccessary and give it a correct naming?

Thanks

Code

[Parameter(ParameterSetName='Get', Mandatory)]

[Parameter(ParameterSetName='Get1', Mandatory)]

[Parameter(ParameterSetName = 'List', Mandatory)]

[Parameter(ParameterSetName='List1', Mandatory)]

[Sample.API.Category('Path')] [System.String] ${userId},

[Parameter(ParameterSetName = 'Get', Mandatory)]

[Parameter(ParameterSetName = 'Get1', Mandatory)]

[Sample.API.Category('Path')] [System.String] ${taskId},

With code above I achive cmdlet -> Get-Task -UserId "stringId" and base path 2 that calls -> mypage.com/my/saas/api/users/{userId}/tasks/ that gets all tasks for userId

JnaneshD05 commented 2 years ago

Is there any steps to publish the generated modules to gallery? Please share

dicolanl commented 1 year ago

Hi Does anyone have a sample for handling response? eg we want to change some fields that are returned from a int enum to a string or change a timestamp from epoch to something human readable.

arnydo commented 1 year ago

Does anyone have any tips on addressing this error:

error   | Modeler | Boolean schema 'schemas:657' with unknown format: 'bool' is not valid
error   | Modeler/UnknownSchemaType | The model schemas:657 does not have a recognized schema type 'boolean'
IzaacJ commented 5 months ago

~New to autorest, and am running into issues regarding circular references, and am completely lost on how I setup the directive no-inline thing. Is it in the swagger.json, if so, where, or is it in another file, and if so, where and what name should it have? Been searching through issues both here and swagger, but unable to find any clear documentation for it.~

Stumbled upon it when just skimming through the docs, for the nth time. https://github.com/Azure/autorest/blob/main/docs/generate/readme.md#keeping-your-options-in-one-place-the-preferred-option

Another issue has showed up thou, generated variable name starts with a number, and is therefore invalid C#. It is in generated/cmdlets directory, and it seems to have only happened in the only file that ends with ViaIdentity and a double-digit number. Any other generated cmdlets that have a double-digit number but without ViaIdentity does not suffer from this issue. None of the generated files with ViaIdentity and a single-digit number have this issue.

The generated code is below:

/// <summary>Identity Parameter</summary>
        [global::System.Management.Automation.Parameter(Mandatory = true, HelpMessage = "Identity Parameter", ValueFromPipeline = true)]
        [global::OurModule.Category(global::OurModule.ParameterCategory.Path)]
        public OurModule.Models.IOurModuleIdentity 0InputObject { get => this._0InputObject; set => this._0InputObject = value; }

Edit: Also noticed now that the onCreated and onOk parameters is duplicated in almost every method in generated/api/OurModule.cs. Probably due to a lot of the generated/cmdlets/ files contain duplicate overrideOnCreated, onCreated, overrideOnOk and onOk with the same parameter types.

Edit2: Think I've figured out why there are duplicates of onCreated and onOk. Our API, build with Entity Framework, returns an object with error details (I'm unfamiliar with EF, this might be OOB or not) and in the swagger it correctly specifies that it is returned on a bunch of non 20* codes, but autorest generates a handler for those error objects for both 200 and 201, even if that isn't specified in the swagger.

Example endpoint that generates duplicate onCreated and onOk methods:


    "/api/Function": {
      "get": {
        "tags": [
          "Function"
        ],
        "summary": "Gets all functions asynchronous.",
        "parameters": [
          {
            "name": "ShowRemoved",
            "in": "query",
            "schema": {
              "type": "boolean",
              "default": false
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/FunctionViewListModel"
                  }
                }
              }
            }
          },
          "204": {
            "description": "No Content"
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "Not Found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "422": {
            "description": "Client Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "Function"
        ],
        "summary": "Creates a function asynchronous.",
        "requestBody": {
          "description": "The create function model.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/FunctionCreateModel"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              },
              "application/json": {
                "schema": {
                  "type": "string"
                }
              },
              "text/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "Not Found",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "422": {
            "description": "Client Error",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        }
      }
    },