Skip to content
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

Check result type before using in operator #1724

Merged
merged 2 commits into from
Jul 2, 2024

Conversation

simensol
Copy link
Contributor

Changes

Make sure result is an object before using the in operator. Fixes the following errors when generating a schema:

node_modules/openapi-typescript/dist/transform/schema-object.js:149
                if ("schema" in result) {
                             ^

TypeError: Cannot use 'in' operator to search for 'schema' in UUID
    at transformSchemaObjectCore (node_modules/openapi-typescript/dist/transform/schema-object.js:149:30)
    at transformSchemaObjectWithComposition (node_modules/openapi-typescript/dist/transform/schema-object.js:108:28)
    at transformSchemaObject (node_modules/openapi-typescript/dist/transform/schema-object.js:6:18)
    at transformParameterObject (node_modules/openapi-typescript/dist/transform/parameter-object.js:4:37)
    at transformParametersArray (node_modules/openapi-typescript/dist/transform/parameters-array.js:30:19)
    at transformOperationObject (node_modules/openapi-typescript/dist/transform/operation-object.js:9:18)
    at injectOperationObject (node_modules/openapi-typescript/dist/transform/operation-object.js:38:18)
    at transformPathItemObject (node_modules/openapi-typescript/dist/transform/path-item-object.js:37:13)
    at Object.transformPathsObject [as paths] (node_modules/openapi-typescript/dist/transform/paths-object.js:18:34)
    at transformSchema (node_modules/openapi-typescript/dist/transform/index.js:20:47)
node_modules/openapi-typescript/dist/transform/schema-object.js:298
                        if ("schema" in result) {
                                     ^

TypeError: Cannot use 'in' operator to search for 'schema' in UUID
    at transformSchemaObjectCore (node_modules/openapi-typescript/dist/transform/schema-object.js:298:38)
    at transformSchemaObjectWithComposition (node_modules/openapi-typescript/dist/transform/schema-object.js:108:28)
    at Object.transformSchemaObject [as schemas] (node_modules/openapi-typescript/dist/transform/schema-object.js:6:18)
    at Object.transformComponentsObject [as components] (node_modules/openapi-typescript/dist/transform/components-object.js:25:48)
    at transformSchema (node_modules/openapi-typescript/dist/transform/index.js:20:47)
    at openapiTS (node_modules/openapi-typescript/dist/index.js:65:20)

@simensol simensol requested a review from a team as a code owner June 24, 2024 09:49
@drwpow drwpow closed this Jun 24, 2024
@drwpow drwpow reopened this Jun 24, 2024
@drwpow
Copy link
Contributor

drwpow commented Jun 24, 2024

Thanks @simensol! This fix looks good, but it needs 2 things to be approved:

  1. Test added to guard against regressions
  2. Changeset (pnpm exec changeset from root)

Copy link

changeset-bot bot commented Jun 24, 2024

🦋 Changeset detected

Latest commit: c8475bc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@simensol
Copy link
Contributor Author

I really tried to create a test. But coming from Python, I don't really know the basics of writing TypeScript tests. I managed to run the existing test suite, but I didn't manage to write a test of my change. However, I have created the Changeset as requested.

@drwpow
Copy link
Contributor

drwpow commented Jun 24, 2024

If you’re not able to provide a test, could you provide a schema you’re using, or implementation where this fails? Without a test it’s not clear what scenario leads to this error, since this doesn’t happen in the existing tests.

@simensol
Copy link
Contributor Author

Sure! Here is a minimal schema that raises the error:

openapi: 3.0.3
info:
    title: Test API
    version: 0.1.0
    description: Test
paths:
    /guardians/:
        get:
            operationId: guardians_list
            security:
                - cookieAuth: []
                - basicAuth: []
            responses:
                "200":
                    content:
                        application/json:
                            schema:
                                type: array
                                items:
                                    $ref: "#/components/schemas/Guardian"
components:
    schemas:
        Guardian:
            type: object
            properties:
                children:
                    type: array
                    items:
                        type: string
                        format: uuid

@drwpow
Copy link
Contributor

drwpow commented Jun 24, 2024

Thanks! And I take it you’re using the Node.js API + transform() to turn it into a UUID? Because that functionality doesn’t exist natively without some modification.

Further, is it desirable that in those cases, the default transformer just stop executing if the node has been touched (which I think is what your PR changes, which is preferable to erring out but wanted to double-check)?

@simensol
Copy link
Contributor Author

You're correct (I think). I have added some transformers to handle the special formats decimal and uuid from the schema generated from my Python API:

openapiTS(localPath, {
    transform(schemaObject) {
        if ("format" in schemaObject) {
            if (schemaObject.format === "decimal") {
                return schemaObject.nullable ? "number | null" : "number"
            } else if (schemaObject.format === "uuid") {
                return schemaObject.nullable ? "UUID | null" : "UUID"
            }
        }
    }
}).then((response) => {
    // This will not validate the UUID, only give a hint to the developer. See
    // <https://github.com/Microsoft/TypeScript/issues/6579#issuecomment-218418435> and
    // <https://www.reddit.com/r/typescript/comments/bhvd3w/comment/elw3s61/>.
    response = astToString(response).replace(
        "export interface paths {",
        "export type UUID = string\n\nexport interface paths {"
    )

    fs.writeFileSync(saveApiTypesFilePath, response)
})

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing! Will add tests in a followup.

@drwpow drwpow merged commit f47268f into openapi-ts:main Jul 2, 2024
7 checks passed
@github-actions github-actions bot mentioned this pull request Jul 2, 2024
@simensol simensol deleted the result-fix branch July 2, 2024 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants