Skip to content

Commit ec57b64

Browse files
authored
[#174797437] Use proper parameter name (#202)
1 parent 504a9d5 commit ec57b64

File tree

6 files changed

+117
-7
lines changed

6 files changed

+117
-7
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Generated code is slightly different from `v4` as it implements some bug fixes t
228228
* The above rule doesn't apply to headers: in case of a security definition or a global parameter which has `in: header`, the definition name is considered, as the `name` attribute refers to the actual header name to be used as for OpenApi specification.
229229
* Generated decoders now support multiple success codes (i.e. 200 and 202), so we don't need to write custom decoders for such case as [we used to do](https://github.com/pagopa/io-backend/compare/174376802-experiment-with-sdk?expand=1#diff-cf7a83babfaf6e5babe84dffe22f64e4L81).
230230
* When using `gen-api-models` command, `--request-types` flag must be used explicitly in order to have `requestTypes` file generated.
231+
* Parameters that has a schema reference (like [this](https://github.com/pagopa/io-utils/blob/491d927ff263863bda9038fffa26442050b788e7/__mocks__/api.yaml#L87)) now use the `name` attribute as the parameter name. It used to be the lower-cased reference's name instead.
231232
#### from 4.0.0 to 4.3.0
232233
* Attributes with `type: string` and `format: date` used to result in a `String` definition, while now produce `Date`. [#184](https://github.com/pagopa/io-utils/pull/184)
233234
* Allow camel-cased prop names. [#183](https://github.com/pagopa/io-utils/pull/183)

Diff for: __mocks__/api.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ paths:
7878
type: string
7979
"500":
8080
description: "Fatal error"
81+
/test-parameter-with-reference:
82+
post:
83+
operationId: "testParameterWithReference"
84+
parameters:
85+
- name: createdMessage
86+
in: body
87+
schema:
88+
$ref: "#/definitions/Message"
89+
responses:
90+
"201":
91+
description: "Created"
92+
"500":
93+
description: "Fatal error"
8194

8295
definitions:
8396
AllOfTest:

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

+45-5
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,9 @@ import {
665665
TestFileUploadT,
666666
testFileUploadDefaultDecoder,
667667
TestResponseHeaderT,
668-
testResponseHeaderDefaultDecoder
668+
testResponseHeaderDefaultDecoder,
669+
TestParameterWithReferenceT,
670+
testParameterWithReferenceDefaultDecoder
669671
} from \\"./requestTypes\\";
670672
671673
// This is a placeholder for undefined when dealing with object keys
@@ -677,12 +679,14 @@ type __UNDEFINED_KEY = \\"_____\\";
677679
export type ApiOperation = TypeofApiCall<TestAuthBearerT> &
678680
TypeofApiCall<TestMultipleSuccessT> &
679681
TypeofApiCall<TestFileUploadT> &
680-
TypeofApiCall<TestResponseHeaderT>;
682+
TypeofApiCall<TestResponseHeaderT> &
683+
TypeofApiCall<TestParameterWithReferenceT>;
681684
682685
export type ParamKeys = keyof (TypeofApiParams<TestAuthBearerT> &
683686
TypeofApiParams<TestMultipleSuccessT> &
684687
TypeofApiParams<TestFileUploadT> &
685-
TypeofApiParams<TestResponseHeaderT>);
688+
TypeofApiParams<TestResponseHeaderT> &
689+
TypeofApiParams<TestParameterWithReferenceT>);
686690
687691
/**
688692
* Defines an adapter for TypeofApiCall which omit one or more parameters in the signature
@@ -709,7 +713,8 @@ export type WithDefaultsT<
709713
| TestAuthBearerT
710714
| TestMultipleSuccessT
711715
| TestFileUploadT
712-
| TestResponseHeaderT,
716+
| TestResponseHeaderT
717+
| TestParameterWithReferenceT,
713718
K
714719
>;
715720
@@ -728,6 +733,10 @@ export type Client<
728733
readonly testFileUpload: TypeofApiCall<TestFileUploadT>;
729734
730735
readonly testResponseHeader: TypeofApiCall<TestResponseHeaderT>;
736+
737+
readonly testParameterWithReference: TypeofApiCall<
738+
TestParameterWithReferenceT
739+
>;
731740
}
732741
: {
733742
readonly testAuthBearer: TypeofApiCall<
@@ -757,6 +766,13 @@ export type Client<
757766
Omit<RequestParams<TestResponseHeaderT>, K>
758767
>
759768
>;
769+
770+
readonly testParameterWithReference: TypeofApiCall<
771+
ReplaceRequestParams<
772+
TestParameterWithReferenceT,
773+
Omit<RequestParams<TestParameterWithReferenceT>, K>
774+
>
775+
>;
760776
};
761777
762778
/**
@@ -876,11 +892,35 @@ export function createClient<K extends ParamKeys>({
876892
options
877893
);
878894
895+
const testParameterWithReferenceT: ReplaceRequestParams<
896+
TestParameterWithReferenceT,
897+
RequestParams<TestParameterWithReferenceT>
898+
> = {
899+
method: \\"post\\",
900+
901+
headers: () => ({
902+
\\"Content-Type\\": \\"application/json\\"
903+
}),
904+
response_decoder: testParameterWithReferenceDefaultDecoder(),
905+
url: ({}) => \`\${basePath}/test-parameter-with-reference\`,
906+
907+
body: ({ createdMessage }) => JSON.stringify(createdMessage),
908+
909+
query: () => ({})
910+
};
911+
const testParameterWithReference: TypeofApiCall<TestParameterWithReferenceT> = createFetchRequestForApi(
912+
testParameterWithReferenceT,
913+
options
914+
);
915+
879916
return {
880917
testAuthBearer: (withDefaults || identity)(testAuthBearer),
881918
testMultipleSuccess: (withDefaults || identity)(testMultipleSuccess),
882919
testFileUpload: (withDefaults || identity)(testFileUpload),
883-
testResponseHeader: (withDefaults || identity)(testResponseHeader)
920+
testResponseHeader: (withDefaults || identity)(testResponseHeader),
921+
testParameterWithReference: (withDefaults || identity)(
922+
testParameterWithReference
923+
)
884924
};
885925
}
886926
"

Diff for: src/commands/gen-api-models/__tests__/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ describe("gen-api-models", () => {
510510

511511
const allOperations = parseAllOperations(spec, "undefined", "undefined");
512512

513-
expect(allOperations).toEqual(expected);
513+
expect(allOperations).toEqual(expect.arrayContaining(expected));
514514
});
515515

516516
it("should render a client", async () => {

Diff for: src/commands/gen-api-models/__tests__/parse.test.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* tslint:disable:no-duplicate-string */
2+
3+
import { OpenAPIV2 } from "openapi-types";
4+
import * as SwaggerParser from "swagger-parser";
5+
6+
import {
7+
parseAllOperations,
8+
parseOperation,
9+
} from "../parse";
10+
11+
let spec: OpenAPIV2.Document;
12+
beforeAll(
13+
async () =>
14+
(spec = (await SwaggerParser.bundle(
15+
`${process.cwd()}/__mocks__/api.yaml`
16+
)) as OpenAPIV2.Document)
17+
);
18+
19+
describe("gen-api-models parse", () => {
20+
it("should parse an operation with external parameter", () => {
21+
const parsed = parseOperation(spec, "/test-auth-bearer", [], "undefined", "undefined")("get");
22+
23+
expect(parsed).toEqual(
24+
expect.objectContaining({
25+
method: "get",
26+
path: "/test-auth-bearer",
27+
parameters: expect.arrayContaining([
28+
{ name: "qo?", in: "query", type: "string" },
29+
{ name: "qr", in: "query", type: "string" },
30+
// cursor is defined as external parameter
31+
{ name: "cursor?", in: "query", type: "string" }
32+
])
33+
})
34+
);
35+
})
36+
37+
it("should parse an operation which parameter has schema reference", () => {
38+
const parsed = parseOperation(
39+
spec,
40+
"/test-parameter-with-reference",
41+
[],
42+
"undefined",
43+
"undefined"
44+
)("post");
45+
46+
expect(parsed).toEqual(
47+
expect.objectContaining({
48+
method: "post",
49+
path: "/test-parameter-with-reference",
50+
parameters: expect.arrayContaining([
51+
{ name: "createdMessage?", in: "body", type: "Message" }
52+
])
53+
})
54+
);
55+
});
56+
});

Diff for: src/commands/gen-api-models/parse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ const parseParameter = (
313313
const paramName = `${uncapitalize(
314314
specParameters && specParameters[parsedRef.e2]
315315
? specParameters[parsedRef.e2].name
316-
: parsedRef.e2
316+
: param.name
317317
)}${isParamRequired ? "" : "?"}`;
318318

319319
const paramIn =

0 commit comments

Comments
 (0)