Skip to content

Commit 4238f17

Browse files
authored
[typescript] fix: enum can not receive stringified class name (OpenAPITools#19481)
* fix: `enum` can not receive stringified class name * chore: update generated code samples
1 parent dfc381c commit 4238f17

29 files changed

Lines changed: 1603 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
generatorName: typescript
2+
outputDir: samples/openapi3/client/petstore/typescript/builds/enum
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/enum_mapping_test.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/typescript
5+
additionalProperties:
6+
platform: deno
7+
npmName: ts-petstore-client
8+
projectName: ts-petstore-client
9+
moduleName: petstore

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
6262
{{/discriminatorValue}}
6363
{{/allVars}}
6464
{{#discriminatorName}}
65+
{{^discriminator.isEnum}}
6566
this.{{discriminatorName}} = "{{classname}}";
67+
{{/discriminator.isEnum}}
6668
{{/discriminatorName}}
6769
}
6870
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API to test enum mapping
4+
description: API description in Markdown.
5+
version: 1.0.0
6+
paths:
7+
/interactions:
8+
get:
9+
summary: Returns all interactions.
10+
description: Optional extended description in Markdown.
11+
parameters:
12+
- in: query
13+
name: parameter_type_mapping
14+
schema:
15+
type: string
16+
format: special
17+
responses:
18+
200:
19+
description: OK
20+
content:
21+
application/json:
22+
schema:
23+
$ref: '#/components/schemas/Interaction'
24+
components:
25+
schemas:
26+
ChatMessage:
27+
type: "object"
28+
properties:
29+
type:
30+
description: "The type of interaction"
31+
enum:
32+
- "chat-message"
33+
example: "chat-message"
34+
type: "string"
35+
message:
36+
description: "The message content"
37+
type: "string"
38+
example: "Hello, how are you?"
39+
Email:
40+
type: "object"
41+
properties:
42+
type:
43+
description: "The type of interaction"
44+
enum:
45+
- "email"
46+
example: "email"
47+
type: "string"
48+
subject:
49+
description: "The email subject"
50+
type: "string"
51+
example: "Meeting"
52+
body:
53+
description: "The email body"
54+
type: "string"
55+
example: "Hello, I would like to schedule a meeting with you."
56+
Interaction:
57+
required:
58+
- type
59+
type: "object"
60+
discriminator:
61+
propertyName: "type"
62+
mapping:
63+
chat-message: "#/components/schemas/ChatMessage"
64+
email: "#/components/schemas/Email"
65+
properties:
66+
type:
67+
description: "The type of interaction"
68+
enum:
69+
- "call"
70+
- "email"
71+
- "meeting"
72+
- "chat-message"
73+
example: "meeting"
74+
type: "string"
75+
oneOf:
76+
- $ref: "#/components/schemas/ChatMessage"
77+
- $ref: "#/components/schemas/Email"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.gitignore
2+
DefaultApi.md
3+
apis/DefaultApi.ts
4+
apis/baseapi.ts
5+
apis/exception.ts
6+
auth/auth.ts
7+
configuration.ts
8+
git_push.sh
9+
http/http.ts
10+
http/isomorphic-fetch.ts
11+
index.ts
12+
middleware.ts
13+
models/ChatMessage.ts
14+
models/Email.ts
15+
models/Interaction.ts
16+
models/ObjectSerializer.ts
17+
models/all.ts
18+
rxjsStub.ts
19+
servers.ts
20+
types/ObjectParamAPI.ts
21+
types/ObservableAPI.ts
22+
types/PromiseAPI.ts
23+
util.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.9.0-SNAPSHOT
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# petstore.DefaultApi
2+
3+
All URIs are relative to *http://localhost*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**interactionsGet**](DefaultApi.md#interactionsGet) | **GET** /interactions | Returns all interactions.
8+
9+
10+
# **interactionsGet**
11+
> Interaction interactionsGet()
12+
13+
Optional extended description in Markdown.
14+
15+
### Example
16+
17+
18+
```typescript
19+
import { petstore } from 'ts-petstore-client';
20+
import * as fs from 'fs';
21+
22+
const configuration = petstore.createConfiguration();
23+
const apiInstance = new petstore.DefaultApi(configuration);
24+
25+
let body:petstore.DefaultApiInteractionsGetRequest = {
26+
// string (optional)
27+
parameterTypeMapping: "parameter_type_mapping_example",
28+
};
29+
30+
apiInstance.interactionsGet(body).then((data:any) => {
31+
console.log('API called successfully. Returned data: ' + data);
32+
}).catch((error:any) => console.error(error));
33+
```
34+
35+
36+
### Parameters
37+
38+
Name | Type | Description | Notes
39+
------------- | ------------- | ------------- | -------------
40+
**parameterTypeMapping** | [**string**] | | (optional) defaults to undefined
41+
42+
43+
### Return type
44+
45+
**Interaction**
46+
47+
### Authorization
48+
49+
No authorization required
50+
51+
### HTTP request headers
52+
53+
- **Content-Type**: Not defined
54+
- **Accept**: application/json
55+
56+
57+
### HTTP response details
58+
| Status code | Description | Response headers |
59+
|-------------|-------------|------------------|
60+
**200** | OK | - |
61+
62+
[[Back to top]](#) [[Back to API list]](README.md#documentation-for-api-endpoints) [[Back to Model list]](README.md#documentation-for-models) [[Back to README]](README.md)
63+
64+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// TODO: better import syntax?
2+
import {BaseAPIRequestFactory, RequiredError, COLLECTION_FORMATS} from './baseapi.ts';
3+
import {Configuration} from '../configuration.ts';
4+
import {RequestContext, HttpMethod, ResponseContext, HttpFile, HttpInfo} from '../http/http.ts';
5+
import {ObjectSerializer} from '../models/ObjectSerializer.ts';
6+
import {ApiException} from './exception.ts';
7+
import {canConsumeForm, isCodeInRange} from '../util.ts';
8+
import {SecurityAuthentication} from '../auth/auth.ts';
9+
10+
11+
import { Interaction } from '../models/Interaction.ts';
12+
13+
/**
14+
* no description
15+
*/
16+
export class DefaultApiRequestFactory extends BaseAPIRequestFactory {
17+
18+
/**
19+
* Optional extended description in Markdown.
20+
* Returns all interactions.
21+
* @param parameterTypeMapping
22+
*/
23+
public async interactionsGet(parameterTypeMapping?: string, _options?: Configuration): Promise<RequestContext> {
24+
let _config = _options || this.configuration;
25+
26+
27+
// Path Params
28+
const localVarPath = '/interactions';
29+
30+
// Make Request Context
31+
const requestContext = _config.baseServer.makeRequestContext(localVarPath, HttpMethod.GET);
32+
requestContext.setHeaderParam("Accept", "application/json, */*;q=0.8")
33+
34+
// Query Params
35+
if (parameterTypeMapping !== undefined) {
36+
requestContext.setQueryParam("parameter_type_mapping", ObjectSerializer.serialize(parameterTypeMapping, "string", "special"));
37+
}
38+
39+
40+
41+
const defaultAuth: SecurityAuthentication | undefined = _options?.authMethods?.default || this.configuration?.authMethods?.default
42+
if (defaultAuth?.applySecurityAuthentication) {
43+
await defaultAuth?.applySecurityAuthentication(requestContext);
44+
}
45+
46+
return requestContext;
47+
}
48+
49+
}
50+
51+
export class DefaultApiResponseProcessor {
52+
53+
/**
54+
* Unwraps the actual response sent by the server from the response context and deserializes the response content
55+
* to the expected objects
56+
*
57+
* @params response Response returned by the server for a request to interactionsGet
58+
* @throws ApiException if the response code was not in [200, 299]
59+
*/
60+
public async interactionsGetWithHttpInfo(response: ResponseContext): Promise<HttpInfo<Interaction >> {
61+
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
62+
if (isCodeInRange("200", response.httpStatusCode)) {
63+
const body: Interaction = ObjectSerializer.deserialize(
64+
ObjectSerializer.parse(await response.body.text(), contentType),
65+
"Interaction", ""
66+
) as Interaction;
67+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
68+
}
69+
70+
// Work around for missing responses in specification, e.g. for petstore.yaml
71+
if (response.httpStatusCode >= 200 && response.httpStatusCode <= 299) {
72+
const body: Interaction = ObjectSerializer.deserialize(
73+
ObjectSerializer.parse(await response.body.text(), contentType),
74+
"Interaction", ""
75+
) as Interaction;
76+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
77+
}
78+
79+
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny(), response.headers);
80+
}
81+
82+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Configuration } from '../configuration.ts'
2+
3+
/**
4+
*
5+
* @export
6+
*/
7+
export const COLLECTION_FORMATS = {
8+
csv: ",",
9+
ssv: " ",
10+
tsv: "\t",
11+
pipes: "|",
12+
};
13+
14+
15+
/**
16+
*
17+
* @export
18+
* @class BaseAPI
19+
*/
20+
export class BaseAPIRequestFactory {
21+
22+
constructor(protected configuration: Configuration) {
23+
}
24+
};
25+
26+
/**
27+
*
28+
* @export
29+
* @class RequiredError
30+
* @extends {Error}
31+
*/
32+
export class RequiredError extends Error {
33+
name: "RequiredError" = "RequiredError";
34+
constructor(public api: string, public method: string, public field: string) {
35+
super("Required parameter " + field + " was null or undefined when calling " + api + "." + method + ".");
36+
}
37+
}

0 commit comments

Comments
 (0)