-
-
Notifications
You must be signed in to change notification settings - Fork 543
Fix V2 parameter types #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ea0a6f4
to
2598f10
Compare
Thanks so much! I’ll review it soon |
@drwpow Thanks for you reply! I'll modify tests as well if the direction of this PR is okay. |
src/transform/type.ts
Outdated
} | ||
|
||
function transformItemsObj(obj: ItemsObject): string { | ||
switch (obj.type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code actually already exists in utils.ts#L29. So by re-adding different logic, we could introduce bugs.
Thanks so much for your PR! I think the fix is much easier than expected. We’re using the same code for OpenAPI 3.0 and Swagger 2.0, and the - paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown"
+ if (version === 2) {
+ paramObj.type ? transformSchemaObj(paramObj) : "unknown"
+ } else if (version === 3) {
+ paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown"
+ } So that’s step 1. Step 2 would be to pass that Also, I find it best to start by actually making a new test in |
@drwpow |
Ah so you know what? This problem is a bit trickier than expected. I seemed to miss this critical part of the Open API 2.0 specs that states that we only use the top-level object if Will comment a fix on your PR and I think we can wrap this up! |
src/transform/parameters.ts
Outdated
mappedParams[reference.in][reference.name || paramName] = { | ||
...reference, | ||
schema: { $ref: paramObj.$ref }, | ||
...(version === 2 ? schema : version === 3 ? { schema } : {}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So because this is a bit difficult to follow, let’s take up a little more space:
if (version === 2) {
mappedParams[reference.in][reference.name || paramName] = {
..reference,
$ref: paramObj.$ref,
} as any;
} else if (version === 3) {
mappedParams[reference.in][reference.name || paramName] = {
..reference,
schema: { $ref: paramObj.$ref },
} as any;
}
Your version takes up fewer lines, and is good in that regard. But you have a nested ternary + a spread operator, and it’s very easy for someone to make a mistake (if not you, the next person to touch this).
This version is more verbose, but it’s a bit clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you. Fixed in 8c02bd7
Perhaps we need to consider the case where in
is body
here as well?
src/transform/parameters.ts
Outdated
};\n`; | ||
let type = ``; | ||
if (version === 2) { | ||
type = paramObj.type ? transformSchemaObj(paramObj) : "unknown"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So according to the OpenAPI 2.0 spec, this will cause a regression, which is why the V2 tests are failing (to your credit, I think the specs are very confusing here, and I suspect it’s why they changed it for 3.0 🙂 ).
Instead, this needs to be:
type = paramObj.type ? transformSchemaObj(paramObj) : "unknown"; | |
if (paramObj.in === 'body' && paramObj.schema) { | |
type = transformSchemaObj(paramObj.schema); | |
} else if (paramObj.type) { | |
type = transformSchemaObj(paramObj); | |
} else { | |
type = 'unknown'; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I missed the spec.. fixed in fe3b69b
src/transform/parameters.ts
Outdated
output += ` "${paramName}"${required}: ${ | ||
paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown" | ||
};\n`; | ||
let type = ``; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we use another name, other than type
? Only because type
is a special word in TypeScript (it doesn’t seem to be causing any problems here, but just for clarity)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see! I renamed it to paramType
. bb668ed
src/transform/parameters.ts
Outdated
if (version === 2) { | ||
type = paramObj.type ? transformSchemaObj(paramObj) : "unknown"; | ||
} else if (version === 3) { | ||
type = paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 I believe this is still correct.
items: { | ||
type: "string", | ||
describe.only("transformParametersArray()", () => { | ||
describe("v2", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice tests! 💯
]).trim() | ||
).toBe( | ||
`query: { | ||
{ in: "path", name: "three_d_secure", required: true, type: "string" }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: with our new knowledge, let’s try adding a body
param here, like: { in: "body", schema: { type: "string" } }
. Then we can test if it generated correctly.
@drwpow By the way, do we need to keep the changes I did in |
src/types/index.ts
Outdated
export interface ParameterObject { | ||
name?: string; // required | ||
in?: "query" | "header" | "path" | /* V3 */ "cookie" | /* V2 */ "formData" | /* V2 */ "body"; // required | ||
description?: string; | ||
required?: boolean; | ||
deprecated?: boolean; | ||
schema?: ReferenceObject | SchemaObject; // required | ||
type?: "string" | "number" | "integer" | "boolean" | "array" | "file"; // V2 ONLY | ||
items?: ItemsObject; // V2 ONLY |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be ReferenceObject | SchemaObject
I believe.
src/types/index.ts
Outdated
@@ -60,13 +60,22 @@ export interface OperationObject { | |||
responses?: Record<string, ReferenceObject | ResponseObject>; // required | |||
} | |||
|
|||
export interface ItemsObject { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we can remove this; this seems to be a duplication of SchemaObject
but with fewer fields (see SchemaObject["items"]
as a reference
This is looking great! It looks like the only thing keeping the tests from passing are to take These snapshot tests are bad at testing individual features because there are so many lines. But they are good at making sure that complete schemas are generating correctly. I believe all the changes I see point to your PR working as intended; it looks like we are now at an improvement with no other regressions! I’ll go ahead and approve this, and as soon as you copy the files to |
Codecov Report
@@ Coverage Diff @@
## main #489 +/- ##
==========================================
+ Coverage 84.61% 84.82% +0.21%
==========================================
Files 9 9
Lines 312 323 +11
Branches 98 105 +7
==========================================
+ Hits 264 274 +10
- Misses 45 46 +1
Partials 3 3
Continue to review full report at Codecov.
|
This looks great, thank you! |
@all-contributors please add @yamacent for code, bug, test |
I've put up a pull request to add @yamacent! 🎉 |
@drwpow Once again, thank you for your kind review! |
This was just released in |
close #470