Skip to content

[FIX][typescript-fetch] Fix duplicate imports for models with a discriminator #19195

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/configs/typescript-fetch-self-import-issue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
generatorName: typescript-fetch
outputDir: samples/client/others/typescript-fetch/self-import-issue
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-fetch/self-import-issue.yaml
templateDir: modules/openapi-generator/src/main/resources/typescript-fetch
additionalProperties:
typescriptThreePlus: "true"
legacyDiscriminatorBehavior: "false"
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Objects.nonNull;
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
import static org.openapitools.codegen.utils.StringUtils.*;

Expand Down Expand Up @@ -387,8 +388,32 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
for (ModelsMap entry : result.values()) {
for (ModelMap model : entry.getModels()) {
ExtendedCodegenModel codegenModel = (ExtendedCodegenModel) model.getModel();
model.put("hasImports", codegenModel.imports.size() > 0);
boolean importsPresent = !codegenModel.imports.isEmpty();

// When legacyDiscriminatorBehaviour = false, DefaultCodegen will add the mapped models of the
// discriminator to codegenModel.imports, causing us to duplicate the import if we don't remove them
CodegenDiscriminator discriminator = codegenModel.discriminator;
boolean mappedDiscriminatorModelsPresent = nonNull(discriminator)
&& nonNull(discriminator.getMappedModels());
if (importsPresent && mappedDiscriminatorModelsPresent) {
Set<String> mappedDiscriminatorModelNames = discriminator.getMappedModels()
.stream()
.map(CodegenDiscriminator.MappedModel::getModelName)
.collect(Collectors.toSet());
Set<String> filteredImports = codegenModel.imports
.stream()
.filter(modelImport ->
!mappedDiscriminatorModelNames.contains(modelImport))
.collect(Collectors.toSet());

codegenModel.imports.clear();
codegenModel.imports.addAll(filteredImports);
}

model.put("hasImports", importsPresent);
model.put("tsImports", toTsImports(codegenModel, parseImports(codegenModel)));


allModels.add(codegenModel);
if (codegenModel.isEntity) {
entityModelClassnames.add(codegenModel.classname);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: "3.0.1"
info:
title: Example
version: "1"
paths: {}
components:
schemas:
BranchDto:
type: object
properties:
name:
type: string
AbstractUserDto:
type: object
properties:
username:
type: string
branch:
"$ref": "#/components/schemas/BranchDto"
type:
type: string
discriminator:
propertyName: type
mapping:
internal-authenticated: "#/components/schemas/InternalAuthenticatedUserDto"
remote-authenticated: "#/components/schemas/RemoteAuthenticatedUserDto"
InternalAuthenticatedUserDto:
type: object
allOf:
- "$ref": "#/components/schemas/AbstractUserDto"
RemoteAuthenticatedUserDto:
type: object
allOf:
- "$ref": "#/components/schemas/AbstractUserDto"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
index.ts
models/AbstractUserDto.ts
models/BranchDto.ts
models/InternalAuthenticatedUserDto.ts
models/RemoteAuthenticatedUserDto.ts
models/index.ts
runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.8.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* tslint:disable */
/* eslint-disable */
export * from './runtime';
export * from './models/index';
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* tslint:disable */
/* eslint-disable */
/**
* Example
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { mapValues } from '../runtime';
import type { BranchDto } from './BranchDto';
import {
BranchDtoFromJSON,
BranchDtoFromJSONTyped,
BranchDtoToJSON,
} from './BranchDto';

import { InternalAuthenticatedUserDtoFromJSONTyped } from './InternalAuthenticatedUserDto';
import { RemoteAuthenticatedUserDtoFromJSONTyped } from './RemoteAuthenticatedUserDto';
/**
*
* @export
* @interface AbstractUserDto
*/
export interface AbstractUserDto {
/**
*
* @type {string}
* @memberof AbstractUserDto
*/
username?: string;
/**
*
* @type {BranchDto}
* @memberof AbstractUserDto
*/
branch?: BranchDto;
/**
*
* @type {string}
* @memberof AbstractUserDto
*/
type?: string;
}

/**
* Check if a given object implements the AbstractUserDto interface.
*/
export function instanceOfAbstractUserDto(value: object): value is AbstractUserDto {
return true;
}

export function AbstractUserDtoFromJSON(json: any): AbstractUserDto {
return AbstractUserDtoFromJSONTyped(json, false);
}

export function AbstractUserDtoFromJSONTyped(json: any, ignoreDiscriminator: boolean): AbstractUserDto {
if (json == null) {
return json;
}
if (!ignoreDiscriminator) {
if (json['type'] === 'internal-authenticated') {
return InternalAuthenticatedUserDtoFromJSONTyped(json, true);
}
if (json['type'] === 'remote-authenticated') {
return RemoteAuthenticatedUserDtoFromJSONTyped(json, true);
}
}
return {

'username': json['username'] == null ? undefined : json['username'],
'branch': json['branch'] == null ? undefined : BranchDtoFromJSON(json['branch']),
'type': json['type'] == null ? undefined : json['type'],
};
}

export function AbstractUserDtoToJSON(value?: AbstractUserDto | null): any {
if (value == null) {
return value;
}
return {

'username': value['username'],
'branch': BranchDtoToJSON(value['branch']),
'type': value['type'],
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* tslint:disable */
/* eslint-disable */
/**
* Example
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { mapValues } from '../runtime';
/**
*
* @export
* @interface BranchDto
*/
export interface BranchDto {
/**
*
* @type {string}
* @memberof BranchDto
*/
name?: string;
}

/**
* Check if a given object implements the BranchDto interface.
*/
export function instanceOfBranchDto(value: object): value is BranchDto {
return true;
}

export function BranchDtoFromJSON(json: any): BranchDto {
return BranchDtoFromJSONTyped(json, false);
}

export function BranchDtoFromJSONTyped(json: any, ignoreDiscriminator: boolean): BranchDto {
if (json == null) {
return json;
}
return {

'name': json['name'] == null ? undefined : json['name'],
};
}

export function BranchDtoToJSON(value?: BranchDto | null): any {
if (value == null) {
return value;
}
return {

'name': value['name'],
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* tslint:disable */
/* eslint-disable */
/**
* Example
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { mapValues } from '../runtime';
import type { BranchDto } from './BranchDto';
import {
BranchDtoFromJSON,
BranchDtoFromJSONTyped,
BranchDtoToJSON,
} from './BranchDto';
import type { AbstractUserDto } from './AbstractUserDto';
import {
AbstractUserDtoFromJSON,
AbstractUserDtoFromJSONTyped,
AbstractUserDtoToJSON,
} from './AbstractUserDto';

/**
*
* @export
* @interface InternalAuthenticatedUserDto
*/
export interface InternalAuthenticatedUserDto extends AbstractUserDto {
}

/**
* Check if a given object implements the InternalAuthenticatedUserDto interface.
*/
export function instanceOfInternalAuthenticatedUserDto(value: object): value is InternalAuthenticatedUserDto {
return true;
}

export function InternalAuthenticatedUserDtoFromJSON(json: any): InternalAuthenticatedUserDto {
return InternalAuthenticatedUserDtoFromJSONTyped(json, false);
}

export function InternalAuthenticatedUserDtoFromJSONTyped(json: any, ignoreDiscriminator: boolean): InternalAuthenticatedUserDto {
return json;
}

export function InternalAuthenticatedUserDtoToJSON(value?: InternalAuthenticatedUserDto | null): any {
return value;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* tslint:disable */
/* eslint-disable */
/**
* Example
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { mapValues } from '../runtime';
import type { BranchDto } from './BranchDto';
import {
BranchDtoFromJSON,
BranchDtoFromJSONTyped,
BranchDtoToJSON,
} from './BranchDto';
import type { AbstractUserDto } from './AbstractUserDto';
import {
AbstractUserDtoFromJSON,
AbstractUserDtoFromJSONTyped,
AbstractUserDtoToJSON,
} from './AbstractUserDto';

/**
*
* @export
* @interface RemoteAuthenticatedUserDto
*/
export interface RemoteAuthenticatedUserDto extends AbstractUserDto {
}

/**
* Check if a given object implements the RemoteAuthenticatedUserDto interface.
*/
export function instanceOfRemoteAuthenticatedUserDto(value: object): value is RemoteAuthenticatedUserDto {
return true;
}

export function RemoteAuthenticatedUserDtoFromJSON(json: any): RemoteAuthenticatedUserDto {
return RemoteAuthenticatedUserDtoFromJSONTyped(json, false);
}

export function RemoteAuthenticatedUserDtoFromJSONTyped(json: any, ignoreDiscriminator: boolean): RemoteAuthenticatedUserDto {
return json;
}

export function RemoteAuthenticatedUserDtoToJSON(value?: RemoteAuthenticatedUserDto | null): any {
return value;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* tslint:disable */
/* eslint-disable */
export * from './AbstractUserDto';
export * from './BranchDto';
export * from './InternalAuthenticatedUserDto';
export * from './RemoteAuthenticatedUserDto';
Loading
Loading