OpenAPITools / OpenAPITools/openapi-generator

[BUG] [CSharp-netcore] model.vars.dataType is not equivalent to model.vars.items.dataType when you have a List of Dictionary

Open
#13,678 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When I have a model with a property that is a List of Dictionary<x,y> the generated code is public List<Dictionary> MyProperty {get; set;} instead of public List<Dictionary<x,y>> MyProperty {get;set;} causing compilation issues (Using the generic type 'Dictionary<TKey, TValue>' requires 2 type arguments) . I have debugged the model and I noticed that model.vars.dataType is incorrect but model.vars.items.dataType is correct:

...
     "name" : "ApplicationLayout",
    "classname" : "ApplicationLayout",
    "classVarName" : "ApplicationLayout",
    "modelJson" : "{\r\n  \"type\" : \"object\",\r\n  \"properties\" : {\r\n    \"elements\" : {\r\n      \"type\" : \"array\",\r\n      \"items\" : {\r\n        \"$ref\" : \"#/components/schemas/ApplicationLayoutElements\"\r\n      }\r\n    },\r\n    \"label\" : {\r\n      \"type\" : \"string\"\r\n    },\r\n    \"options\" : {\r\n      \"type\" : \"object\",\r\n      \"additionalProperties\" : { }\r\n    },\r\n    \"rule\" : {\r\n      \"$ref\" : \"#/components/schemas/ApplicationLayout_rule\"\r\n    },\r\n    \"scope\" : {\r\n      \"type\" : \"string\"\r\n    },\r\n    \"type\" : {\r\n      \"type\" : \"string\"\r\n    }\r\n  }\r\n}",
    "dataType" : "Object",
    "classFilename" : "ApplicationLayout",
    "isAlias" : false,
    "isString" : false,
    "isInteger" : false,
    "isLong" : false,
    "isNumber" : false,
    "isNumeric" : false,
    "isFloat" : false,
    "isDouble" : false,
    "isDate" : false,
    "isDateTime" : false,
    "isDecimal" : false,
    "isShort" : false,
    "isUnboundedInteger" : false,
    "isPrimitiveType" : false,
    "isBoolean" : false,
    "additionalPropertiesIsAnyType" : false,
    "vars" : [ {
      "openApiType" : "array",
      "baseName" : "elements",
      "getter" : "getElements",
      "setter" : "setElements",
      "dataType" : "List<Dictionary>", <-- INCORRECT
      "datatypeWithEnum" : "List<Dictionary>", <-- INCORRECT
      "name" : "Elements",
      "defaultValueWithParam" : " = data.elements;",
      "baseType" : "List",
      "containerType" : "array",
      "jsonSchema" : "{\r\n  \"type\" : \"array\",\r\n  \"items\" : {\r\n    \"$ref\" : \"#/components/schemas/ApplicationLayoutElements\"\r\n  }\r\n}",
      "exclusiveMinimum" : false,
      "exclusiveMaximum" : false,
      "required" : false,
      "deprecated" : false,
      "hasMoreNonReadOnly" : false,
      "isPrimitiveType" : true,
      "isModel" : false,
      "isContainer" : true,
      "isString" : false,
      "isNumeric" : false,
      "isInteger" : false,
      "isShort" : false,
      "isLong" : false,
      "isUnboundedInteger" : false,
      "isNumber" : false,
      "isFloat" : false,
      "isDouble" : false,
      "isDecimal" : false,
      "isByteArray" : false,
      "isBinary" : false,
      "isFile" : false,
      "isBoolean" : false,
      "isDate" : false,
      "isDateTime" : false,
      "isUuid" : false,
      "isUri" : false,
      "isEmail" : false,
      "isNull" : false,
      "isFreeFormObject" : false,
      "isAnyType" : false,
      "isArray" : true,
      "isMap" : false,
      "isEnum" : false,
      "isReadOnly" : false,
      "isWriteOnly" : false,
      "isNullable" : false,
      "isSelfReference" : false,
      "isCircularReference" : false,
      "isDiscriminator" : false,
      "items" : {
        "openApiType" : "object",
        "baseName" : "Elements",
        "getter" : "getElements",
        "setter" : "setElements",
        "dataType" : "Dictionary<string, Object>", <-- CORRECT
        "datatypeWithEnum" : "Dictionary<string, Object>", <-- CORRECT
        "name" : "Elements",
        "defaultValueWithParam" : " = data.Elements;",
        "baseType" : "Dictionary",
        "containerType" : "map",
        "jsonSchema" : "{\r\n  \"type\" : \"object\",\r\n  \"additionalProperties\" : true\r\n}",
...
openapi-generator version

6.2.0

OpenAPI declaration file content or url
Generation Details

openapi-generator-cli generate -g csharp-netcore -i input.yaml

Steps to reproduce

1- Use the spec provided above
2- Run openapi-generator-cli generate -g csharp-netcore -i input.yaml
3- Verify the generated code

Related issues/PRs
Suggest a fix

It seems that model.vars.items.dataType looks fine so I'd suggest porting the same logic to model.vars.dataType

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running openapi-generator-cli generate -g csharp-netcore -i input.yaml with the supplied OpenAPI spec and inspect the generated property type. Trace how the generator represents the array item type versus the container data type. Done means the generated property uses List<Dictionary<string, Object>> and compiles without the missing type-argument error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.