swagger-api / swagger-api/swagger-codegen

[typescript-angular] special characters like $ are removed from var names

Open
#7,484 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

When generating TS client from a swagger spec, special characters (like $) are removed. This is caused by variable name sanitization.

This problem didn't exist in 2.2.3 and appeared since commit e70bdf17f21fcf0315d03d0846f6c47c818bd43e(caused by call to sanitizeName method)

Sanitization should depend on target language, as allowed characters in variable names are language dependant.

Spec example :

definitions: {
	Hateoas.Link: {
		type: "object",
		properties: {
			rel: {
				type: "string"
			},
			verb: {
				type: "string"
			},
			href: {
				type: "string"
			},
			title: {
				type: "string"
			}
		}
	},
	Hateoas.Resource: {
		required: [
			"$type"
		],
		type: "object",
		properties: {
			links: {
				type: "array",
				items: {
					$ref: "#/definitions/Hateoas.Link"
				}
			},
			$type: {
				type: "string"
			}
		},
		discriminator: "$type"
	},
}

In TS file :

export interface HateoasResource {
    links?: Array<HateoasLink>;
    type: string;
}

Should be something like :

export interface HateoasResource {
    links?: Array<HateoasLink>;
    $type: string;
}

Suggestion :
Make function sanitizeName (in DefaultCodegen.java) virtual and override it in AbstractTypeScriptClientCodegen.java removing (or correcting) name = name.replaceAll("\\W", "");

Thanks for your work

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

Read sanitizeName in DefaultCodegen.java and its suggested TypeScript override point in AbstractTypeScriptClientCodegen.java. Reproduce issue 7484 with the shown Hateoas.Resource specification, then verify that generated TypeScript preserves the $type property name while remaining valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.