Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface {{classname}}Interface {
* @throws {RequiredError}
* @memberof {{classname}}Interface
*/
{{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.RequestOpts>;
{{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): {{#hasAuthMethods}}Promise<{{/hasAuthMethods}}runtime.RequestOpts{{#hasAuthMethods}}>{{/hasAuthMethods}};

/**
* {{&notes}}
Expand Down Expand Up @@ -113,7 +113,7 @@ export class {{classname}} extends runtime.BaseAPI {
* @deprecated
{{/isDeprecated}}
*/
async {{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.RequestOpts> {
{{#hasAuthMethods}}async {{/hasAuthMethods}}{{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): {{#hasAuthMethods}}Promise<{{/hasAuthMethods}}runtime.RequestOpts{{#hasAuthMethods}}>{{/hasAuthMethods}} {
{{#allParams}}
{{#required}}
if (requestParameters['{{paramName}}'] == null) {
Expand Down Expand Up @@ -360,7 +360,7 @@ export class {{classname}} extends runtime.BaseAPI {
{{/isDeprecated}}
*/
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request, {{/allParams.0}}initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
const requestOptions = await this.{{nickname}}RequestOpts({{#allParams.0}}requestParameters{{/allParams.0}});
const requestOptions = {{#hasAuthMethods}}await {{/hasAuthMethods}}this.{{nickname}}RequestOpts({{#allParams.0}}requestParameters{{/allParams.0}});
const response = await this.request(requestOptions, initOverrides);

{{#returnType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,30 @@ public void testValidationAttributesWithWithoutRuntimeChecks() throws IOExceptio
TestUtils.assertFileContains(modelsIndex, "[property: string]:");
}

/**
* Endpoints without auth methods should generate synchronous RequestOpts functions,
* while endpoints with auth methods should generate async RequestOpts functions.
*/
@Test(description = "Verify RequestOpts is only async when auth methods are present")
public void testRequestOptsIsAsyncOnlyWithAuthMethods() throws IOException {
File output = generate(
Collections.emptyMap(),
"src/test/resources/3_0/typescript-fetch/api-with-and-without-auth.yaml"
);

Path apiFile = Paths.get(output + "/apis/DefaultApi.ts");
TestUtils.assertFileExists(apiFile);

// Endpoint without auth: synchronous signature and no await at call site
TestUtils.assertFileContains(apiFile, "listPublicItemsRequestOpts(): runtime.RequestOpts {");
TestUtils.assertFileNotContains(apiFile, "listPublicItemsRequestOpts(): Promise<runtime.RequestOpts>");
TestUtils.assertFileContains(apiFile, "const requestOptions = this.listPublicItemsRequestOpts()");

// Endpoint with auth: async signature and await at call site
TestUtils.assertFileContains(apiFile, "async listPrivateItemsRequestOpts(): Promise<runtime.RequestOpts> {");
TestUtils.assertFileContains(apiFile, "const requestOptions = await this.listPrivateItemsRequestOpts()");
}

private static File generate(
Map<String, Object> properties
) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.0.3
info:
title: API with mixed auth
version: 1.0.0
paths:
/public/items:
get:
operationId: listPublicItems
summary: List public items (no auth)
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Item'
/private/items:
get:
operationId: listPrivateItems
summary: List private items (requires API key)
security:
- ApiKeyAuth: []
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Item'
components:
securitySchemes:
ApiKeyAuth:
type: apiKey
in: header
name: X-API-KEY
schemas:
Item:
type: object
required:
- id
- name
properties:
id:
type: string
name:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
* Creates request options for list without sending the request
*/
async listRequestOpts(requestParameters: ListRequest): Promise<runtime.RequestOpts> {
listRequestOpts(requestParameters: ListRequest): runtime.RequestOpts {
if (requestParameters['personId'] == null) {
throw new runtime.RequiredError(
'personId',
Expand All @@ -61,7 +61,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
*/
async listRaw(requestParameters: ListRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Club>> {
const requestOptions = await this.listRequestOpts(requestParameters);
const requestOptions = this.listRequestOpts(requestParameters);
const response = await this.request(requestOptions, initOverrides);

return new runtime.JSONApiResponse(response, (jsonValue) => ClubFromJSON(jsonValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
* Creates request options for list without sending the request
*/
async listRequestOpts(requestParameters: ListRequest): Promise<runtime.RequestOpts> {
listRequestOpts(requestParameters: ListRequest): runtime.RequestOpts {
if (requestParameters['personId'] == null) {
throw new runtime.RequiredError(
'personId',
Expand All @@ -61,7 +61,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
*/
async listRaw(requestParameters: ListRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Club>> {
const requestOptions = await this.listRequestOpts(requestParameters);
const requestOptions = this.listRequestOpts(requestParameters);
const response = await this.request(requestOptions, initOverrides);

return new runtime.JSONApiResponse(response, (jsonValue) => ClubFromJSON(jsonValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class AnotherFakeApi extends runtime.BaseAPI {
/**
* Creates request options for _123testSpecialTags without sending the request
*/
async _123testSpecialTagsRequestOpts(requestParameters: 123testSpecialTagsRequest): Promise<runtime.RequestOpts> {
_123testSpecialTagsRequestOpts(requestParameters: 123testSpecialTagsRequest): runtime.RequestOpts {
if (requestParameters['client'] == null) {
throw new runtime.RequiredError(
'client',
Expand Down Expand Up @@ -65,7 +65,7 @@ export class AnotherFakeApi extends runtime.BaseAPI {
* To test special tags
*/
async _123testSpecialTagsRaw(requestParameters: 123testSpecialTagsRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Client>> {
const requestOptions = await this._123testSpecialTagsRequestOpts(requestParameters);
const requestOptions = this._123testSpecialTagsRequestOpts(requestParameters);
const response = await this.request(requestOptions, initOverrides);

return new runtime.JSONApiResponse(response, (jsonValue) => ClientFromJSON(jsonValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
* Creates request options for fooGet without sending the request
*/
async fooGetRequestOpts(): Promise<runtime.RequestOpts> {
fooGetRequestOpts(): runtime.RequestOpts {
const queryParameters: any = {};

const headerParameters: runtime.HTTPHeaders = {};
Expand All @@ -49,7 +49,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
*/
async fooGetRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<FooGetDefaultResponse>> {
const requestOptions = await this.fooGetRequestOpts();
const requestOptions = this.fooGetRequestOpts();
const response = await this.request(requestOptions, initOverrides);

return new runtime.JSONApiResponse(response, (jsonValue) => FooGetDefaultResponseFromJSON(jsonValue));
Expand Down
Loading
Loading