Skip to content

transform function changing objects to not-nullable no longer working in v7 #1758

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

Closed
1 of 2 tasks
IGx89 opened this issue Jul 11, 2024 · 3 comments
Closed
1 of 2 tasks
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@IGx89
Copy link
Contributor

IGx89 commented Jul 11, 2024

Description

Hello! When my OpenAPI v3.0 spec has something like this:

  "foo": {
      "allOf": [
          {
              "$ref": "#/components/schemas/Bar"
          }
      ],
      "nullable": true
  },

And my transform is this:

    transform(schemaObject) {
      // Treat optional AND nullable fields as just optional (possibly undefined)
      if (!schemaObject.required && schemaObject.nullable) {
        schemaObject.nullable = false;
      }
    },

with v6 I was getting the following:

foo?: components["schemas"]["Bar"];

but with v7 I'm now getting:

foo?: components["schemas"]["Bar"] | null;

Is this expected behavior? What can I do to make v7 work like v6 did here? The logic appears to still work for primitive type fields, but no longer for object refs.

Name Version
openapi-typescript 7.0.2
Node.js 20.15.1
OS + version Windows 11

Checklist

@IGx89 IGx89 added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jul 11, 2024
@phk422
Copy link
Contributor

phk422 commented Jul 12, 2024

Description

Hello! When my OpenAPI v3.0 spec has something like this:

  "foo": {
      "allOf": [
          {
              "$ref": "#/components/schemas/Bar"
          }
      ],
      "nullable": true
  },

And my transform is this:

    transform(schemaObject) {
      // Treat optional AND nullable fields as just optional (possibly undefined)
      if (!schemaObject.required && schemaObject.nullable) {
        schemaObject.nullable = false;
      }
    },

with v6 I was getting the following:

foo?: components["schemas"]["Bar"];

but with v7 I'm now getting:

foo?: components["schemas"]["Bar"] | null;

Is this expected behavior? What can I do to make v7 work like v6 did here? The logic appears to still work for primitive type fields, but no longer for object refs.

Name Version
openapi-typescript 7.0.2
Node.js 20.15.1
OS + version Windows 11
Checklist

In V7, the transform operates after the type generation, so attempting to change schemaObject directly will not take effect. but, you can use the following method to modify the type, I think it's very flexible:

import transformSchemaObject from 'openapi-typescript/dist/transform/schema-object.ts'

function transform(schemaObject, options) {
  // Treat optional AND nullable fields as just optional (possibly undefined)
  if (!schemaObject.required && schemaObject.nullable) {
    schemaObject.nullable = false;
    return {
      schema: transformSchemaObject(schemaObject, options),
      questionToken: true
    }
  }
}

@IGx89
Copy link
Contributor Author

IGx89 commented Jul 12, 2024

Thanks a lot! Would not have figured that out myself, don't see that function (nor the odd import) in the docs. Setting questionToken to true broke other things (foo?: string became foo?: string | undefined) but the following worked:

      if (!schemaObject.required && schemaObject.nullable) {
        schemaObject.nullable = false;
        return transformSchemaObject(schemaObject, options);
      }

@IGx89 IGx89 closed this as completed Jul 12, 2024
@phk422
Copy link
Contributor

phk422 commented Jul 13, 2024

transformSchemaObject

This weird function import I mentioned a pr for - #1759

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

2 participants