Skip to content

Commit 0ed4755

Browse files
authored
chore: openapi-generator 의 몇몇 옵션값을 cherry-pick 합니다 (#10)
* feat(openapi): bfc784e openapi-generator 8에 들어가게되는 OpenAPITools#18531 를 채리픽 합니다. * fix(typescript-client): TypeScript Client 의 codegen 이 'from' 을 예약어 취급해서 제네래이션을 막는 버그 수정 * chore: example update * chore: docsgen 누락 수정 * fix: 기본값수정 * chore: test 누락 수정 * fix: whatwg-fetch 의존성 제거 full-context: 현제 니즈에서는 whatwg-fetch 만을 포함할수 있는 상황도 아니며 재품 기준에서 처리하기 어려움 * fix(openapi-typescript): auth methods configuration 타입젠이 잘못된 securitySchemes 를 만나면 타입의 값이 비어버리는 버그 픽스 * chore: update diff
1 parent e7dae1a commit 0ed4755

File tree

37 files changed

+87
-73
lines changed

37 files changed

+87
-73
lines changed

bin/configs/typescript-consolidated-browser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ additionalProperties:
88
projectName: ts-petstore-client
99
moduleName: petstore
1010
supportsES6: true
11+
enumType: stringUnion

bin/configs/typescript-consolidated-deno.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ additionalProperties:
77
npmName: ts-petstore-client
88
projectName: ts-petstore-client
99
moduleName: petstore
10+
enumType: stringUnion

bin/configs/typescript-consolidated-jquery.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ additionalProperties:
77
npmName: ts-petstore-client
88
projectName: ts-petstore-client
99
moduleName: petstore
10+
enumType: stringUnion

bin/configs/typescript-consolidated-node-object-parameters.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ additionalProperties:
88
useObjectParameters: true
99
projectName: ts-petstore-client
1010
moduleName: petstore
11+
enumType: stringUnion

docs/generators/typescript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2424
|enumNameSuffix|Suffix that will be appended to all enum names.| |Enum|
2525
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase|
2626
|enumPropertyNamingReplaceSpecialChar|Set to true to replace '-' and '+' symbols with 'minus_' and 'plus_' in enum of type string| |false|
27+
|enumType|Specify the enum type which should be used in the client code.|<dl><dt>**stringUnion**</dt><dd>Union of literal string types</dd><dt>**enum**</dt><dd>Typescript's [string enums](https://www.typescriptlang.org/docs/handbook/enums.html#string-enums)</dd></dl>|enum|
2728
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
2829
|fileContentDataType|Specifies the type to use for the content of a file - i.e. Blob (Browser, Deno) / Buffer (node)| |Buffer|
2930
|framework|Specify the framework which should be used in the client code.|<dl><dt>**fetch-api**</dt><dd>fetch-api</dd><dt>**jquery**</dt><dd>jquery</dd></dl>|fetch-api|
@@ -136,7 +137,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
136137
<li>float</li>
137138
<li>for</li>
138139
<li>formParams</li>
139-
<li>from</li>
140140
<li>function</li>
141141
<li>goto</li>
142142
<li>headerParams</li>

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.swagger.v3.oas.models.media.Schema;
2727
import io.swagger.v3.oas.models.parameters.Parameter;
2828
import io.swagger.v3.oas.models.parameters.RequestBody;
29+
import io.swagger.v3.oas.models.security.SecurityScheme;
2930
import lombok.Getter;
3031
import lombok.Setter;
3132
import org.apache.commons.lang3.StringUtils;
@@ -81,6 +82,10 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp
8182
public static final String USE_ERASABLE_SYNTAX = "useErasableSyntax";
8283
public static final String USE_ERASABLE_SYNTAX_DESC = "Use erasable syntax for the generated code. This is a temporary feature and will be removed in the future.";
8384

85+
private static final String ENUM_TYPE_SWITCH = "enumType";
86+
private static final String ENUM_TYPE_SWITCH_DESC = "Specify the enum type which should be used in the client code.";
87+
private static final String[][] ENUM_TYPES = {{"stringUnion", "Union of literal string types"}, {"enum", "Typescript's [string enums](https://www.typescriptlang.org/docs/handbook/enums.html#string-enums)"}};
88+
8489
private final Map<String, String> frameworkToHttpLibMap;
8590

8691
@Setter
@@ -118,8 +123,6 @@ public TypeScriptClientCodegen() {
118123

119124
// NOTE: TypeScript uses camel cased reserved words, while models are title cased. We don't want lowercase comparisons.
120125
reservedWords.addAll(Arrays.asList(
121-
// local variable names used in API methods (endpoints)
122-
"from",
123126
// Typescript reserved words
124127
"constructor"));
125128

@@ -149,6 +152,13 @@ public TypeScriptClientCodegen() {
149152

150153
cliOptions.add(platformOption);
151154

155+
CliOption enumTypeOption = new CliOption(TypeScriptClientCodegen.ENUM_TYPE_SWITCH, TypeScriptClientCodegen.ENUM_TYPE_SWITCH_DESC);
156+
for (String[] option : TypeScriptClientCodegen.ENUM_TYPES) {
157+
enumTypeOption.addEnum(option[0], option[1]);
158+
}
159+
enumTypeOption.defaultValue(ENUM_TYPES[1][0]);
160+
cliOptions.add(enumTypeOption);
161+
152162
// Set property naming to camelCase
153163
supportModelPropertyNaming(CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.camelCase);
154164

@@ -402,6 +412,14 @@ public ModelsMap postProcessModels(ModelsMap objs) {
402412
return objs;
403413
}
404414

415+
@Override
416+
public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> schemes) {
417+
List<CodegenSecurity> securities = super.fromSecurity(schemes);
418+
419+
securities.removeIf(security ->!security.isApiKey && !security.isBasicBasic && !security.isBasicBearer && !security.isOAuth && !security.isHttpSignature);
420+
return securities;
421+
}
422+
405423
@Override
406424
public String apiDocFileFolder() {
407425
return (outputFolder + "/" + apiDocPath).replace('/', File.separatorChar);
@@ -450,6 +468,15 @@ public void processOpts() {
450468
"http", httpLibName + ".ts"
451469
));
452470

471+
additionalProperties.putIfAbsent(ENUM_TYPE_SWITCH, ENUM_TYPES[1][0]);
472+
Object propEnumType = additionalProperties.get(ENUM_TYPE_SWITCH);
473+
474+
Map<String, Boolean> enumTypes = new HashMap<>();
475+
for (String[] option : ENUM_TYPES) {
476+
enumTypes.put(option[0], option[0].equals(propEnumType));
477+
}
478+
additionalProperties.put("enumTypes", enumTypes);
479+
453480
Object propPlatform = additionalProperties.get(PLATFORM_SWITCH);
454481
if (propPlatform == null) {
455482
propPlatform = "browser";

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.swagger.v3.oas.models.parameters.Parameter;
2525
import io.swagger.v3.oas.models.parameters.RequestBody;
2626
import io.swagger.v3.oas.models.responses.ApiResponse;
27+
import io.swagger.v3.oas.models.security.SecurityScheme;
2728
import io.swagger.v3.oas.models.servers.Server;
2829
import io.swagger.v3.parser.util.SchemaTypeUtil;
2930
import lombok.Getter;
@@ -408,6 +409,14 @@ public void postProcessParameter(CodegenParameter parameter) {
408409
}
409410
}
410411

412+
@Override
413+
public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> schemes) {
414+
List<CodegenSecurity> securities = super.fromSecurity(schemes);
415+
416+
securities.removeIf(security ->!security.isApiKey && !security.isBasicBasic && !security.isBasicBearer && !security.isOAuth && !security.isHttpSignature);
417+
return securities;
418+
}
419+
411420
@Override
412421
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
413422
List<ExtendedCodegenModel> allModels = new ArrayList<>();

modules/openapi-generator/src/main/resources/typescript/http/isomorphic-fetch.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { from, Observable } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../r
55
import fetch from "node-fetch";
66
{{/node}}
77
{{#browser}}
8-
import "whatwg-fetch";
8+
//import "whatwg-fetch"; 추가 의존성을 재거하는 방향으로 수정
99
{{/browser}}
1010
{{/platforms}}
1111

modules/openapi-generator/src/main/resources/typescript/model/model.mustache

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,41 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
8989

9090
{{#vars}}
9191
{{#isEnum}}
92+
{{#enumTypes}}
93+
{{#enum}}
9294
export enum {{classname}}{{enumName}} {
9395
{{#allowableValues}}
9496
{{#enumVars}}
9597
{{name}} = {{{value}}}{{^-last}},{{/-last}}
9698
{{/enumVars}}
9799
{{/allowableValues}}
98100
}
101+
{{/enum}}
102+
{{#stringUnion}}
103+
export type {{classname}}{{enumName}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
104+
{{/stringUnion}}
105+
{{/enumTypes}}
99106
{{/isEnum}}
100107
{{/vars}}
101108

102109
{{/hasEnums}}
103110
{{/oneOf}}
104111
{{/isEnum}}
105112
{{#isEnum}}
113+
{{#enumTypes}}
114+
{{#enum}}
106115
export enum {{classname}} {
107116
{{#allowableValues}}
108117
{{#enumVars}}
109118
{{name}} = {{{value}}}{{^-last}},{{/-last}}
110119
{{/enumVars}}
111120
{{/allowableValues}}
112121
}
122+
{{/enum}}
123+
{{#stringUnion}}
124+
export type {{classname}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
125+
{{/stringUnion}}
126+
{{/enumTypes}}
113127
{{/isEnum}}
114128
{{/model}}
115129
{{/models}}

modules/openapi-generator/src/main/resources/typescript/package.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@types/node-fetch": "^2.6.13",
5151
{{/node}}
5252
{{#browser}}
53-
"whatwg-fetch": "^3.0.0",
5453
{{/browser}}
5554
{{/platforms}}
5655
{{/fetch-api}}

0 commit comments

Comments
 (0)