Skip to content

type: "object" being parsed as empty object #1031

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
jackstar12 opened this issue Feb 11, 2023 · 3 comments · Fixed by #1032
Closed

type: "object" being parsed as empty object #1031

jackstar12 opened this issue Feb 11, 2023 · 3 comments · Fixed by #1032
Labels
bug Something isn't working

Comments

@jackstar12
Copy link

Description

A brief description of the issue.
A schema of type: "object" with no other information specified is being parsed as an empty (Record<string, never>) object, but I would expect it to be an Record<string, any>

I think it would have to be changed in here, correct me if I'm wrong:
https://github.com/drwpow/openapi-typescript/blob/3ffb475d15352ebc1dd2f823b01d3ae8b18ef68b/src/transform/schema-object.ts#L244

Name Version
openapi-typescript 6.1.0
Node.js v16.13.2
OS + version Windows 10

OpenAPI
image

Generated TS

    DocumentModel: {
      /** Type */
      type?: string;
      /** Content */
      content?: (components["schemas"]["DocumentModel"])[];
      /** Text */
      text?: string;
      /** Attrs */
      attrs?: Record<string, never>;
      /** Marks */
      marks?: (components["schemas"]["Mark"])[];
    };

*Expected TS

    DocumentModel: {
      /** Type */
      type?: string;
      /** Content */
      content?: (components["schemas"]["DocumentModel"])[];
      /** Text */
      text?: string;
      /** Attrs */
      attrs?: Record<string, any>;
      /** Marks */
      marks?: (components["schemas"]["Mark"])[];
    };

Checklist

  • [x ] My OpenAPI schema passes a validator
  • [x ] I’m willing to open a PR for this (see CONTRIBUTING.md)
@jackstar12 jackstar12 added the bug Something isn't working label Feb 11, 2023
@mitchell-merry
Copy link
Contributor

Disagree. In my opinion it should just generate to the object type. That way, if you try to access a property on the object, you need to validate that it both exists first and that the type is what you expect.

Alternatively, I could see Record<string, unknown> so you at least have to prove the type. I really dislike the idea of introducing anys.

@jackstar12
Copy link
Author

That makes sense, yeah.
A Record<string, unknown> would fix my issue aswell and describes the situation better without introducing an any.

Would the fix be as simple as changing the fallback type in the line mentioned above?

@duncanbeevers
Copy link
Contributor

I needed this for work, so I went ahead and opened a PR with the Record<string, unknown> behavior.

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

Successfully merging a pull request may close this issue.

3 participants