swagger-codegen-generators icon indicating copy to clipboard operation
swagger-codegen-generators copied to clipboard

Typescript instantiationType for maps

Open FranPregernik opened this issue 5 years ago • 2 comments

Hi!

I have an issue where an CodegenModel for a map-like object is incorrectly generated. I am not sure which way to go from here. Is it the generated OASv3 model or the code generator for typescript. I would appreciate any guidance from this community.

I generated a specification for a spring boot project of mine with springdoc. There I use spring data rest which generates APIs with Link/Links: https://github.com/spring-projects/spring-hateoas/blob/1.1.x/src/main/java/org/springframework/hateoas/Links.java#L44

There is a customization in springdoc for these:

		ResolvedSchema resolvedLinkSchema = ModelConverters.getInstance()
				.resolveAsResolvedSchema(new AnnotatedType(Link.class));

		return openApi -> openApi
				.schema("Link", resolvedLinkSchema.schema)
				.schema("Links", new MapSchema()
						.additionalProperties(new StringSchema())
						.additionalProperties(new ObjectSchema().$ref("#/components/schemas/Link")));

This is the OASv3 spec excerpt for just the Links.

class MapSchema {
        class Schema {
            type: object
            format: null
            $ref: null
            description: null
            title: null
            multipleOf: null
            maximum: null
            exclusiveMaximum: null
            minimum: null
            exclusiveMinimum: null
            maxLength: null
            minLength: null
            pattern: null
            maxItems: null
            minItems: null
            uniqueItems: null
            maxProperties: null
            minProperties: null
            required: null
            not: null
            properties: null
            additionalProperties: class Schema {
                type: null
                format: null
                $ref: #/components/schemas/Link
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
            nullable: null
            readOnly: null
            writeOnly: null
            example: null
            externalDocs: null
            deprecated: null
            discriminator: null
            xml: null
        }
    }

When generating the typescript-fetch template I get to the method io.swagger.codegen.v3.generators.DefaultCodegenConfig#toInstantiationType. This method gets the map key from instantiationTypes as seen here https://github.com/swagger-api/swagger-codegen-generators/blob/76ea8a89df73928b10e0c84f1c8356739e34bf3d/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java#L1021

This is null because the map key is never set seen here https://github.com/swagger-api/swagger-codegen-generators/blob/2a561d8587e5bea502941e6c5377ce89eff6f13d/src/main/java/io/swagger/codegen/v3/generators/typescript/AbstractTypeScriptClientCodegen.java#L86

So CodegenModel.parent becomes null<String, Link> which causes the generated model to be like this:

export interface Links extends null<String, Link> {
    [key: string]: Link;
}

I think the proper typescript way would just be:

export interface Links {
    [key: string]: Link;
}

or maybe alternatively, keeping extends:

export interface Links extends Map<String, Link> {
    [key: string]: Link;
}

There are some issues regarding the springdoc generated OASv3 spec like https://github.com/springdoc/springdoc-openapi/issues/524.

FranPregernik avatar May 27 '20 17:05 FranPregernik

Any update on this issue?

magua-io avatar Nov 17 '22 21:11 magua-io

Are there any updates regarding this PR? I have the same issue. It would be really good if this got fixed or at least there would be a workaraound

Paku580 avatar Apr 24 '23 20:04 Paku580