Skip to content

Support for Dictionaries, HashMaps and Associative Arrays #92

@Saegrov

Description

@Saegrov

Swagger has support for describing the types for HashMaps but currently swagger-to-ts only types these as object.

Swaggerdocs on this: https://swagger.io/docs/specification/data-models/dictionaries/

Example

Given this json:

{
  "swagger": "2.0",
  "info": {
    "description": "Api Documentation",
    "version": "1.0",
    "title": "Api Documentation",
    "termsOfService": "urn:tos",
    "contact": {},
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "host": "localhost:8080",
  "basePath": "/",
  "definitions": {
    "CamundaFormField": {
      "type": "object",
      "required": ["displayType", "id", "label", "options", "responseType"],
      "properties": {
        "displayType": {
          "type": "string",
          "enum": ["radio", "date", "select", "textfield", "unknown"]
        },
        "id": { "type": "string" },
        "label": { "type": "string" },
        "options": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        },
        "responseType": {
          "type": "string",
          "enum": [
            "booleanField",
            "stringField",
            "longField",
            "enumField",
            "dateField",
            "customTypeField",
            "unknownFieldType"
          ]
        },
        "value": { "type": "string" }
      },
      "title": "CamundaFormField"
    }
  }
}

Created from this Java class

@ApiModel("CamundaFormField")
public class CamundaFormFieldDto {

    @ApiModelProperty(required = true)
    private String id;

    @ApiModelProperty(required = true)
    private CamundaFormFieldTypeEnum responseType;

    @ApiModelProperty(required = true)
    private String label;

    @ApiModelProperty(required = true)
    private CamundaFormFieldDisplayTypeEnum displayType;

    @ApiModelProperty(required = true)
    private Map<String, String> options;

    private String value;
     /* ... more */ 
}

I get this result where the option property gets typed as object

export interface CamundaFormField {
  displayType: 'radio' | 'date' | 'select' | 'textfield' | 'unknown';
  id: string;
  label: string;
  options: object
  responseType:
    | 'booleanField'
    | 'stringField'
    | 'longField'
    | 'enumField'
    | 'dateField'
    | 'customTypeField'
    | 'unknownFieldType';
  value?: string;
}

The result I wanted in this case was:

export interface CamundaFormField {
  displayType: 'radio' | 'date' | 'select' | 'textfield' | 'unknown';
  id: string;
  label: string;
  options: Record<string, string>
  responseType:
    | 'booleanField'
    | 'stringField'
    | 'longField'
    | 'enumField'
    | 'dateField'
    | 'customTypeField'
    | 'unknownFieldType';
  value?: string;
}

Or if you want to support Typescript lower than 2.1:

export interface CamundaFormField {
  displayType: 'radio' | 'date' | 'select' | 'textfield' | 'unknown';
  id: string;
  label: string;
  options: {[key:string]: string}
  responseType:
    | 'booleanField'
    | 'stringField'
    | 'longField'
    | 'enumField'
    | 'dateField'
    | 'customTypeField'
    | 'unknownFieldType';
  value?: string;
}

I'm happy to provide a PR if you want. Great library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions