Skip to content

Generated code does not compile when additionalProperties is not false #5261

@KirkMunroSagent

Description

@KirkMunroSagent

Describe the bug

When generating code from a yaml or json doc, where that doc does not explicitly specify that additionalProperties is false, and when the endpoint accepts an untyped object input, the generated code will not compile.

Version of NSwag toolchain, computer and .NET runtime used

This issue occurs with 14.13.0. I cannot update to 14.4.0 or 14.5.0 due to regressions introduced in those two releases.

To Reproduce

{
  "openapi": "3.0.1",
  "info": {
    "title": "My WebServices",
    "description": "My WebServices",
    "version": "1.0"
  },
  "servers": [
    {
      "url": "https://myservices.mysite.com/my-service"
    }
  ],
  "paths": {
    "/api/thing/createorupdate": {
      "post": {
        "tags": [],
        "summary": "/api/thing/createorupdate - POST",
        "operationId": "post-api-thing-createorupdate",
        "parameters": [
          {
            "name": "value",
            "in": "query",
            "schema": {
              "type": "object",
              "nullable": true
            }
          },
          {
            "name": "template",
            "in": "query",
            "schema": {
              "type": "string",
              "nullable": true
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ThingResponse"
                },
                "example": {
                  "status": 0,
                  "description": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ThingResponse": {
        "type": "object",
        "properties": {
          "status": {
            "type": "integer",
            "format": "int32"
          },
          "description": {
            "type": "string",
            "nullable": true
          }
        }
      }
   },
    "securitySchemes": {
      "apiKeyHeader": {
        "type": "apiKey",
        "name": "My-Api-Key",
        "in": "header"
      }
    }
  },
  "security": [
    {
      "apiKeyHeader": [ ]
    }
  ]
}

The issue seems to occur because nswag recognizes that the input of type object is not a value type, and as a result it applies this code in the generated endpoint:

if (value != null)
{
    foreach (var item_ in value.AdditionalProperties)
    {
        urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key)).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
    }
}

Since the object? value input is just an object and not a type defined in the json document, there is no AdditionalProperties property, and as a result the generated code does not compile.

Expected behavior

When generating a library when additionalProperties is either not provided (defaults to true) or is explicitly set to true, the code generator must not include code for appending AdditionalProperties on inputs of type object or object?.

Additional context

I think I've given enough detail to identify the issue. If not, let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions