Azure / autorest

OpenAPI (f.k.a Swagger) Specification code generator. Supports C#, PowerShell, Go, Java, Node.js, TypeScript, Python
MIT License
4.55k stars 728 forks source link

[Regression] Modeler ArgumentNullException with request body parameters #3441

Open kevinoid opened 4 years ago

kevinoid commented 4 years ago

Attempting to generate code for an OpenAPI 3.0 document with a multipart/form-data request body, such as the following:

openapi: '3.0.3'
info:
  title: OpenAPI 3 example with multipart/form-data request body
  version: '1.0.0'
components:
  schemas:
    ExampleFormDataBody:
      type: object
      properties:
        prop1:
          type: string
        prop2:
          type: string
paths:
  /post-example:
    post:
      operationId: postExample
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/ExampleFormDataBody'
      responses:
        '201':
          description: Success

Using the command autorest --v3 --csharp --input-file=openapi.yaml, produces an error such as:

AutoRest code generation utility [cli version: 3.0.6187; node: v10.17.0, max-memory: 8192 gb]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
   Loading AutoRest core      '/home/kevin/.autorest/@autorest_core@3.0.6246/node_modules/@autorest/core/dist' (3.0.6246)
   Loading AutoRest extension '@microsoft.azure/autorest.csharp' (~2.3.79->2.3.84)
   Loading AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.55->2.3.55)
FATAL: System.ArgumentNullException: Value cannot be null.
Parameter name: source
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable1 source, Func2 selector)
   at AutoRest.Modeler.Model.RequestBody.AsParameters() in /opt/vsts/work/1/s/src/Model/RequestBody.cs:line 38
   at AutoRest.Modeler.SwaggerModeler.Unwrap(SwaggerParameter swaggerParameter) in /opt/vsts/work/1/s/src/SwaggerModeler.cs:line 452
   at AutoRest.Modeler.OperationBuilder.BuildMethodParameters(Method method) in /opt/vsts/work/1/s/src/OperationBuilder.cs:line 255
   at AutoRest.Modeler.OperationBuilder.BuildMethod(HttpMethod httpMethod, String url, String methodName, String methodGroup) in /opt/vsts/work/1/s/src/OperationBuilder.cs:line 100
   at AutoRest.Modeler.SwaggerModeler.BuildMethod(HttpMethod httpMethod, String url, String name, Operation operation) in /opt/vsts/work/1/s/src/SwaggerModeler.cs:line 404
   at AutoRest.Modeler.SwaggerModeler.Build(ServiceDefinition serviceDefinition) in /opt/vsts/work/1/s/src/SwaggerModeler.cs:line 106
   at AutoRest.Modeler.Program.<ProcessInternal>d__2.MoveNext() in /opt/vsts/work/1/s/src/Program.cs:line 60
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NewPlugin.<Process>d__15.MoveNext()
  Error: Plugin imodeler1 reported failure.

Thanks, Kevin

kevinoid commented 4 years ago

I just noticed this issue also affects OpenAPI 2 documents. For example:

OpenAPI 2 example with formData parameters ```yaml swagger: '2.0' info: title: OpenAPI 2 example with multipart/form-data request body version: 1.0.0 definitions: ExampleFormDataBody: properties: prop1: type: string prop2: type: string type: object paths: /post-example: post: consumes: - multipart/form-data parameters: - in: formData name: prop1 type: string - in: formData name: prop2 type: string responses: '201': description: Success operationId: postExample ```

I also bisected the affected versions and found that --version:3.0.6130 works correctly while--version:3.0.6132 does not.

daviwil commented 3 years ago

See also #3464.

rosieks commented 3 years ago

Any idea which version to use if I face this error and also https://github.com/Azure/autorest/issues/3492?

toymachiner62 commented 3 years ago

I'm also receiving this

I just noticed this issue also affects OpenAPI 2 documents. For example:

OpenAPI 2 example with formData parameters I also bisected the affected versions and found that --version:3.0.6130 works correctly while--version:3.0.6132 does not.

I faced this same issue with an openapi version 3.0.1 spec. I came across your answer and spot on. Up to 3.0.6130 it works. Anything newer than that including 3.0.6247 creates an error.

UncleFirefox commented 3 years ago

Hi, we're also getting the additionalProperties problem doing autorest --input-file=swagger.json --output-folder=. --typescript --version=3.0.6274 anybody looking at it?

maximiliancsuk commented 3 years ago

The issue still exists in the latest version:

$ autorest readme.md --go --v3 --verbose
AutoRest code generation utility [cli version: 3.1.5; node: v12.9.0, max-memory: 1024 MB]
...
FATAL: System.ArgumentNullException: Value cannot be null.
Parameter name: source
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable1 source, Func2 selector)
   at AutoRest.Modeler.Model.RequestBody.AsParameters() in C:\Users\ci\AppData\Local\Temp\PUBLISH8kiit\38_20171116T010915\autorest.modeler\src\Model\RequestBody.cs:line 38
   at AutoRest.Modeler.SwaggerModeler.Unwrap(SwaggerParameter swaggerParameter) in C:\Users\ci\AppData\Local\Temp\PUBLISH8kiit\38_20171116T010915\autorest.modeler\src\SwaggerModeler.cs:line 375
   at AutoRest.Modeler.OperationBuilder.BuildMethodParameters(Method method) in C:\Users\ci\AppData\Local\Temp\PUBLISH8kiit\38_20171116T010915\autorest.modeler\src\OperationBuilder.cs:line 240
   at AutoRest.Modeler.OperationBuilder.BuildMethod(HttpMethod httpMethod, String url, String methodName, String methodGroup) in C:\Users\ci\AppData\Local\Temp\PUBLISH8kiit\38_20171116T010915\autorest.modeler\src\OperationBuilder.cs:line 86
   at AutoRest.Modeler.SwaggerModeler.BuildMethod(HttpMethod httpMethod, String url, String name, Operation operation) in C:\Users\ci\AppData\Local\Temp\PUBLISH8kiit\38_20171116T010915\autorest.modeler\src\SwaggerModeler.cs:line 327
   at AutoRest.Modeler.SwaggerModeler.Build(ServiceDefinition serviceDefinition) in C:\Users\ci\AppData\Local\Temp\PUBLISH8kiit\38_20171116T010915\autorest.modeler\src\SwaggerModeler.cs:line 105
   at AutoRest.Modeler.Program.<ProcessInternal>d__2.MoveNext() in C:\Users\ci\AppData\Local\Temp\PUBLISH8kiit\38_20171116T010915\autorest.modeler\src\Program.cs:line 60
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NewPlugin.<Process>d__15.MoveNext()
  Error: Plugin imodeler1 reported failure.

I tried a bunch of different settings and it seems like it occurs when you use a "multipart/form-data" content. The relevant code part that seems to fail is here: https://github.com/Azure/autorest.modeler/blob/master/src/Model/RequestBody.cs#L38

It's strange because AFAICT, schema.Properties should not be null as the schema definitely contains a Properties member and the call to .Select() should hence succeed... or maybe it cannot parse the properties object in the first place and that's why it's null?

Moving back to an older version - as suggested - is not really a viable workaround for us because that produces a bunch of other errors when generating, so...

Any help?

Relevant parts of api schema:

...
"/api/v{version}/ingest/genericJSON/files": {
      "post": {
        "tags": [
          "OKPluginGenericJSONIngest"
        ],
        "operationId": "Ingest",
        "parameters": [
          {
            "name": "context",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "version",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "required": [
                  "files"
                ],
                "type": "object",
                "properties": {
                  "files": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "binary"
                    }
                  }
                }
              },
              "encoding": {
                "files": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "security": [
          {
            "oauth2": [

            ]
          }
        ]
      }
    },
...
babayevanurana commented 2 years ago

Goid

babayevanurana commented 2 years ago

Good