zijianhuang / openapiclientgen

Generate strongly typed C# and TypeScript client codes from Open API / Swagger definitions supporting jQuery, Angular, AXIOS, Fetch API, Aurelia and Angular Strictly Typed Forms
MIT License
64 stars 13 forks source link

Use specified value type for dictionary properties. #40

Closed codertimu closed 2 years ago

codertimu commented 2 years ago

The generator seems to not take the value type for the dictionaries defined in the spec into account. Take the following example:

openapi: 3.0.3
info:
  title: Test openapi spec file for dictionary field
  version: v1
paths:
  '/hello':
    get:
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TestModel'

components:
  schemas:
    TestModel:
      description: Model information
      properties:
        stringDict:
          type: object
          additionalProperties:
            type: string
        intDict:
          type: object
          additionalProperties:
            type: integer
        objectDict:
          type: object
          additionalProperties:
            type: object

It produces the following class where the value type of the Dictionaries is always object whereas it should be string, int, and object respectively.

namespace MyNS
{

    /// <summary>
    /// Model information
    /// </summary>
    public class TestModel
    {

        [System.Text.Json.Serialization.JsonPropertyName("stringDict")]
        public System.Collections.Generic.Dictionary<string, object> StringDict { get; set; }

        [System.Text.Json.Serialization.JsonPropertyName("intDict")]
        public System.Collections.Generic.Dictionary<string, object> IntDict { get; set; }

        [System.Text.Json.Serialization.JsonPropertyName("objectDict")]
        public System.Collections.Generic.Dictionary<string, object> ObjectDict { get; set; }
    }
}

I have used the following settings:

{
  "ClientNamespace": "MyNS",
  "UseSystemTextJson": true,
  "UsePascalCase": true,
  "DecorateDataModelWithPropertyName": true
}

With this PR, it produces the dictionaries with the correct types specified in the spec as follows:

namespace MyNS
{

    /// <summary>
    /// Model information
    /// </summary>
    public class TestModel
    {

        [System.Text.Json.Serialization.JsonPropertyName("stringDict")]
        public System.Collections.Generic.Dictionary<string, string> StringDict { get; set; }

        [System.Text.Json.Serialization.JsonPropertyName("intDict")]
        public System.Collections.Generic.Dictionary<string, int> IntDict { get; set; }

        [System.Text.Json.Serialization.JsonPropertyName("objectDict")]
        public System.Collections.Generic.Dictionary<string, object> ObjectDict { get; set; }
    }
}