swagger-api / swagger-api/swagger-codegen-generators

Typescript instantiationType for maps

Open
#691 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

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.

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 with DefaultCodegenConfig#toInstantiationType and the instantiationTypes setup in AbstractTypeScriptClientCodegen.java, then reproduce the typescript-fetch generation using the provided Links schema. Compare the generated Links interface with the expected TypeScript output and verify that it no longer contains a null parent type.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.