Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript instantiationType for maps #691

Open
FranPregernik opened this issue May 27, 2020 · 2 comments · May be fixed by #877
Open

Typescript instantiationType for maps #691

FranPregernik opened this issue May 27, 2020 · 2 comments · May be fixed by #877

Comments

@FranPregernik
Copy link

FranPregernik commented May 27, 2020

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

return instantiationTypes.get("map") + "<String, " + inner + ">";

This is null because the map key is never set seen here

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 springdoc/springdoc-openapi#524.

@magua-io
Copy link

Any update on this issue?

@Paku580
Copy link

Paku580 commented Apr 24, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants