Skip to content

Commit a574698

Browse files
authored
fix optional header name (#320)
1 parent be3c5df commit a574698

4 files changed

Lines changed: 113 additions & 16 deletions

File tree

__mocks__/api.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,19 @@ paths:
295295
responses:
296296
"200":
297297
description: "Ok"
298+
/test-header-optional:
299+
get:
300+
operationId: "testHeaderOptional"
301+
parameters:
302+
- name: param
303+
in: header
304+
required: false
305+
schema:
306+
$ref: "#/definitions/CustomStringFormatTest"
307+
responses:
308+
"200":
309+
description: "Ok"
310+
298311
definitions:
299312
Person:
300313
$ref: "definitions.yaml#/Person"

__mocks__/openapi_v3/api.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ paths:
323323
responses:
324324
"200":
325325
description: "Ok"
326+
/test-header-optional:
327+
get:
328+
operationId: "testHeaderOptional"
329+
parameters:
330+
- name: param
331+
in: header
332+
required: false
333+
schema:
334+
$ref: "#/components/schemas/CustomStringFormatTest"
335+
responses:
336+
"200":
337+
description: "Ok"
326338

327339
# -------------
328340
# Components

src/commands/gen-api-models/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,9 @@ import {
936936
TestParamWithSchemaRefT,
937937
testParamWithSchemaRefDefaultDecoder,
938938
TestHeaderWithSchemaRefT,
939-
testHeaderWithSchemaRefDefaultDecoder
939+
testHeaderWithSchemaRefDefaultDecoder,
940+
TestHeaderOptionalT,
941+
testHeaderOptionalDefaultDecoder
940942
} from \\"./requestTypes\\";
941943

942944
// This is a placeholder for undefined when dealing with object keys
@@ -963,7 +965,8 @@ export type ApiOperation = TypeofApiCall<TestAuthBearerT> &
963965
TypeofApiCall<TestCustomTokenHeaderT> &
964966
TypeofApiCall<TestWithEmptyResponseT> &
965967
TypeofApiCall<TestParamWithSchemaRefT> &
966-
TypeofApiCall<TestHeaderWithSchemaRefT>;
968+
TypeofApiCall<TestHeaderWithSchemaRefT> &
969+
TypeofApiCall<TestHeaderOptionalT>;
967970

968971
export type ParamKeys = keyof (TypeofApiParams<TestAuthBearerT> &
969972
TypeofApiParams<TestSimpleTokenT> &
@@ -983,7 +986,8 @@ export type ParamKeys = keyof (TypeofApiParams<TestAuthBearerT> &
983986
TypeofApiParams<TestCustomTokenHeaderT> &
984987
TypeofApiParams<TestWithEmptyResponseT> &
985988
TypeofApiParams<TestParamWithSchemaRefT> &
986-
TypeofApiParams<TestHeaderWithSchemaRefT>);
989+
TypeofApiParams<TestHeaderWithSchemaRefT> &
990+
TypeofApiParams<TestHeaderOptionalT>);
987991

988992
/**
989993
* Defines an adapter for TypeofApiCall which omit one or more parameters in the signature
@@ -1025,7 +1029,8 @@ export type WithDefaultsT<
10251029
| TestCustomTokenHeaderT
10261030
| TestWithEmptyResponseT
10271031
| TestParamWithSchemaRefT
1028-
| TestHeaderWithSchemaRefT,
1032+
| TestHeaderWithSchemaRefT
1033+
| TestHeaderOptionalT,
10291034
K
10301035
>;
10311036

@@ -1084,6 +1089,8 @@ export type Client<
10841089
readonly testParamWithSchemaRef: TypeofApiCall<TestParamWithSchemaRefT>;
10851090

10861091
readonly testHeaderWithSchemaRef: TypeofApiCall<TestHeaderWithSchemaRefT>;
1092+
1093+
readonly testHeaderOptional: TypeofApiCall<TestHeaderOptionalT>;
10871094
}
10881095
: {
10891096
readonly testAuthBearer: TypeofApiCall<
@@ -1218,6 +1225,13 @@ export type Client<
12181225
Omit<RequestParams<TestHeaderWithSchemaRefT>, K>
12191226
>
12201227
>;
1228+
1229+
readonly testHeaderOptional: TypeofApiCall<
1230+
ReplaceRequestParams<
1231+
TestHeaderOptionalT,
1232+
Omit<RequestParams<TestHeaderOptionalT>, K>
1233+
>
1234+
>;
12211235
};
12221236

12231237
/**
@@ -1669,6 +1683,25 @@ export function createClient<K extends ParamKeys>({
16691683
options
16701684
);
16711685

1686+
const testHeaderOptionalT: ReplaceRequestParams<
1687+
TestHeaderOptionalT,
1688+
RequestParams<TestHeaderOptionalT>
1689+
> = {
1690+
method: \\"get\\",
1691+
1692+
headers: ({ [\\"param\\"]: param }) => ({
1693+
param: param
1694+
}),
1695+
response_decoder: testHeaderOptionalDefaultDecoder(),
1696+
url: ({}) => \`\${basePath}/test-header-optional\`,
1697+
1698+
query: () => withoutUndefinedValues({})
1699+
};
1700+
const testHeaderOptional: TypeofApiCall<TestHeaderOptionalT> = createFetchRequestForApi(
1701+
testHeaderOptionalT,
1702+
options
1703+
);
1704+
16721705
return {
16731706
testAuthBearer: (withDefaults || identity)(testAuthBearer),
16741707
testSimpleToken: (withDefaults || identity)(testSimpleToken),
@@ -1698,7 +1731,10 @@ export function createClient<K extends ParamKeys>({
16981731
testCustomTokenHeader: (withDefaults || identity)(testCustomTokenHeader),
16991732
testWithEmptyResponse: (withDefaults || identity)(testWithEmptyResponse),
17001733
testParamWithSchemaRef: (withDefaults || identity)(testParamWithSchemaRef),
1701-
testHeaderWithSchemaRef: (withDefaults || identity)(testHeaderWithSchemaRef)
1734+
testHeaderWithSchemaRef: (withDefaults || identity)(
1735+
testHeaderWithSchemaRef
1736+
),
1737+
testHeaderOptional: (withDefaults || identity)(testHeaderOptional)
17021738
};
17031739
}
17041740
"
@@ -2774,7 +2810,9 @@ import {
27742810
TestParamWithSchemaRefT,
27752811
testParamWithSchemaRefDefaultDecoder,
27762812
TestHeaderWithSchemaRefT,
2777-
testHeaderWithSchemaRefDefaultDecoder
2813+
testHeaderWithSchemaRefDefaultDecoder,
2814+
TestHeaderOptionalT,
2815+
testHeaderOptionalDefaultDecoder
27782816
} from \\"./requestTypes\\";
27792817

27802818
// This is a placeholder for undefined when dealing with object keys
@@ -2802,7 +2840,8 @@ export type ApiOperation = TypeofApiCall<TestAuthBearerT> &
28022840
TypeofApiCall<TestCustomTokenHeaderT> &
28032841
TypeofApiCall<TestWithEmptyResponseT> &
28042842
TypeofApiCall<TestParamWithSchemaRefT> &
2805-
TypeofApiCall<TestHeaderWithSchemaRefT>;
2843+
TypeofApiCall<TestHeaderWithSchemaRefT> &
2844+
TypeofApiCall<TestHeaderOptionalT>;
28062845

28072846
export type ParamKeys = keyof (TypeofApiParams<TestAuthBearerT> &
28082847
TypeofApiParams<TestAuthBearerHttpT> &
@@ -2823,7 +2862,8 @@ export type ParamKeys = keyof (TypeofApiParams<TestAuthBearerT> &
28232862
TypeofApiParams<TestCustomTokenHeaderT> &
28242863
TypeofApiParams<TestWithEmptyResponseT> &
28252864
TypeofApiParams<TestParamWithSchemaRefT> &
2826-
TypeofApiParams<TestHeaderWithSchemaRefT>);
2865+
TypeofApiParams<TestHeaderWithSchemaRefT> &
2866+
TypeofApiParams<TestHeaderOptionalT>);
28272867

28282868
/**
28292869
* Defines an adapter for TypeofApiCall which omit one or more parameters in the signature
@@ -2866,7 +2906,8 @@ export type WithDefaultsT<
28662906
| TestCustomTokenHeaderT
28672907
| TestWithEmptyResponseT
28682908
| TestParamWithSchemaRefT
2869-
| TestHeaderWithSchemaRefT,
2909+
| TestHeaderWithSchemaRefT
2910+
| TestHeaderOptionalT,
28702911
K
28712912
>;
28722913

@@ -2927,6 +2968,8 @@ export type Client<
29272968
readonly testParamWithSchemaRef: TypeofApiCall<TestParamWithSchemaRefT>;
29282969

29292970
readonly testHeaderWithSchemaRef: TypeofApiCall<TestHeaderWithSchemaRefT>;
2971+
2972+
readonly testHeaderOptional: TypeofApiCall<TestHeaderOptionalT>;
29302973
}
29312974
: {
29322975
readonly testAuthBearer: TypeofApiCall<
@@ -3068,6 +3111,13 @@ export type Client<
30683111
Omit<RequestParams<TestHeaderWithSchemaRefT>, K>
30693112
>
30703113
>;
3114+
3115+
readonly testHeaderOptional: TypeofApiCall<
3116+
ReplaceRequestParams<
3117+
TestHeaderOptionalT,
3118+
Omit<RequestParams<TestHeaderOptionalT>, K>
3119+
>
3120+
>;
30713121
};
30723122

30733123
/**
@@ -3549,6 +3599,25 @@ export function createClient<K extends ParamKeys>({
35493599
options
35503600
);
35513601

3602+
const testHeaderOptionalT: ReplaceRequestParams<
3603+
TestHeaderOptionalT,
3604+
RequestParams<TestHeaderOptionalT>
3605+
> = {
3606+
method: \\"get\\",
3607+
3608+
headers: ({ [\\"param\\"]: param }) => ({
3609+
param: param
3610+
}),
3611+
response_decoder: testHeaderOptionalDefaultDecoder(),
3612+
url: ({}) => \`\${basePath}/test-header-optional\`,
3613+
3614+
query: () => withoutUndefinedValues({})
3615+
};
3616+
const testHeaderOptional: TypeofApiCall<TestHeaderOptionalT> = createFetchRequestForApi(
3617+
testHeaderOptionalT,
3618+
options
3619+
);
3620+
35523621
return {
35533622
testAuthBearer: (withDefaults || identity)(testAuthBearer),
35543623
testAuthBearerHttp: (withDefaults || identity)(testAuthBearerHttp),
@@ -3579,7 +3648,10 @@ export function createClient<K extends ParamKeys>({
35793648
testCustomTokenHeader: (withDefaults || identity)(testCustomTokenHeader),
35803649
testWithEmptyResponse: (withDefaults || identity)(testWithEmptyResponse),
35813650
testParamWithSchemaRef: (withDefaults || identity)(testParamWithSchemaRef),
3582-
testHeaderWithSchemaRef: (withDefaults || identity)(testHeaderWithSchemaRef)
3651+
testHeaderWithSchemaRef: (withDefaults || identity)(
3652+
testHeaderWithSchemaRef
3653+
),
3654+
testHeaderOptional: (withDefaults || identity)(testHeaderOptional)
35833655
};
35843656
}
35853657
"

templates/macros.njk

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,13 @@
515515
#}
516516
{% macro tokenize(headerParam) -%}
517517
{% if headerParam.tokenType === "apiKey" and headerParam.authScheme and headerParam.authScheme !== "none" %}
518-
`{{ headerParam.authScheme | capitalizeFirst }} {{ $(headerParam.name | safeIdentifier) }}`
518+
`{{ headerParam.authScheme | capitalizeFirst }} {{ $(headerParam.name | stripQuestionMark | safeIdentifier) }}`
519519
{% elif headerParam.tokenType === "oauth" %}
520-
`Bearer {{ $(headerParam.name | safeIdentifier ) }}`
520+
`Bearer {{ $(headerParam.name | stripQuestionMark | safeIdentifier ) }}`
521521
{% elif headerParam.tokenType === "basic" %}
522-
`Basic {{ $(headerParam.name | safeIdentifier) }}
522+
`Basic {{ $(headerParam.name | stripQuestionMark | safeIdentifier) }}
523523
`{% else %}
524-
{{ headerParam.name | safeIdentifier }}
524+
{{ headerParam.name | stripQuestionMark | safeIdentifier }}
525525
{% endif %}
526526
{% endmacro %}
527527

@@ -552,10 +552,10 @@
552552
{% macro composedHeaderProducers(headerParams, consumes) -%}
553553
(
554554
{% if headerParams.length %}
555-
{ {% for p in headerParams %}{{p.name | safeDestruct | safe }},{% endfor %} }
555+
{ {% for p in headerParams %}{{p.name | stripQuestionMark | safeDestruct | safe }},{% endfor %} }
556556
{% endif %}
557557
) => ({
558-
{% for p in headerParams %}"{{p.headerName}}": {{ tokenize(p) }},{% endfor %}
558+
{% for p in headerParams %}"{{p.headerName | stripQuestionMark }}": {{ tokenize(p) }},{% endfor %}
559559
{% if consumes %}"Content-Type": "{{ consumes }}"{% endif %}
560560
})
561561
{% endmacro %}

0 commit comments

Comments
 (0)