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-Angular2 produces invalid import for self-referencing model #6201

Closed
psamwel opened this issue Jul 27, 2017 · 8 comments
Closed

Comments

@psamwel
Copy link

psamwel commented Jul 27, 2017

Description

typescript-angular2 client codegen for a model that references itself (e.g.):

		"APISearchTerm": {
			"description": "Adds generic filter capability to API",
			"type": "object",
			"properties": {
				"field": {
					"description": "Field to compare",
					"type": "string"
				},
				"fieldValue": {
					"description": "Primary value",
					"type": "string"
				},
				"searchOperation": {
					"description": "Operator type",
					"enum": ["Equal",
					"NotEqual",
					"Contains",
					"GreaterThan",
					"GreaterThanOrEqual",
					"LessThan",
					"LessThanOrEqual"],
					"type": "string"
				},
				"and": {
					"description": "For building nested And expressions",
					"type": "array",
					"items": {
						"$ref": "#/definitions/APISearchTerm"
					}
				},
				"or": {
					"description": "For building nested Or expressions",
					"type": "array",
					"items": {
						"$ref": "#/definitions/APISearchTerm"
					}
				}
			}
		}

resulting in:

import { APISearchTerm } from './aPISearchTerm';

export interface APISearchTerm {
    field?: string;
    fieldValue?: string;
    searchOperation?: APISearchTerm.SearchOperationEnum;
    and?: Array<APISearchTerm>;
    or?: Array<APISearchTerm>;
}
...

which produces the following tsc error(s):

[12:04:12]Error at aPISearchTerm.ts:13:10: Individual declarations in merged declaration 'APISearchTerm' must be all exported or all local.
[12:04:12]Error at aPISearchTerm.ts:13:10: Import declaration conflicts with local declaration of 'APISearchTerm'
[12:04:12]Error at aPISearchTerm.ts:46:18: Individual declarations in merged declaration 'APISearchTerm' must be all exported or all local.

Swagger-codegen version

Verified in latest master & 2.3.0 locally.

2.3.0 branch

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i application.json -l typescript-angular2 -o $outputpath/sdk_angular2 -c $clientConfig

where $clientConfig:
{ "modelPropertyNaming": "original" }

Suggest a fix/enhancement

Exclude a classname from its own list of imports.

@wing328
Copy link
Contributor

wing328 commented Jul 28, 2017

import { APISearchTerm } from './aPISearchTerm';

export interface APISearchTerm {
    field?: string;
    fieldValue?: string;
    searchOperation?: APISearchTerm.SearchOperationEnum;
    and?: Array<APISearchTerm>;
    or?: Array<APISearchTerm>;
}
...

@psamwel thanks for reporting the issue. What should be the correct output look like?

cc @Vrolijkx @taxpon @sebastianhaas @kenisteward @TiFu

@kenisteward
Copy link
Contributor

kenisteward commented Jul 28, 2017 via email

@wing328
Copy link
Contributor

wing328 commented Jul 28, 2017

@kenisteward looks like the issue we can loop through models via postProcessModels and add vendor extensions (e.g. x-ts-angular2-self-ref) to avoid the import.

If anyone has better suggestions, please reply to let us know.

@kenisteward
Copy link
Contributor

kenisteward commented Jul 28, 2017 via email

@wing328
Copy link
Contributor

wing328 commented Jul 28, 2017

@kenisteward Enjoy your vacation 👍

@sebastianhaas
Copy link
Contributor

Isn't this something that could happen in any language that uses imports? It feels weird to add Angular-specific logic for something that might happen in C# etc. as well :/

@kenisteward
Copy link
Contributor

kenisteward commented Jul 31, 2017 via email

@olemarius
Copy link

I'm getting the same issue in typescript-axios (I'm using https://github.com/sius/swagger-nodegen-cli v3.0.34)

image

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

No branches or pull requests

5 participants