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

Add support for @pattern, @format, @nullable #1955

Open
1 task
robogeek opened this issue Oct 21, 2024 · 1 comment
Open
1 task

Add support for @pattern, @format, @nullable #1955

robogeek opened this issue Oct 21, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request openapi-ts Relevant to the openapi-typescript library

Comments

@robogeek
Copy link

Description

The OpenAPI spec I'm working with has pattern, nullable, and format attributes all over the place, and these are not turned into JSDOC tags.

Other tools, like @openapi-codegen/cli, support these tags.

JSDOC tags are used by other tools like ts-to-zod and @savotije/openapi-to-joi, each of which generate schema validation code from TYpeScript source. Both of those recognize @pattern, @nullable and @format tags.

Proposal

I don't really understand the code, but I found some simple changes in addJSDocComment would suffice.

First, I added a pattern field in AnnotatedSchemaObject:

export interface AnnotatedSchemaObject {
  const?: unknown; // jsdoc without value
  default?: unknown; // jsdoc with value
  deprecated?: boolean; // jsdoc without value
  description?: string; // jsdoc with value
  enum?: unknown[]; // jsdoc without value
  example?: string; // jsdoc with value
  format?: string; // not jsdoc
  pattern?: string; // not jsdoc
  nullable?: boolean; // Node information
  summary?: string; // not jsdoc
  title?: string; // not jsdoc
  type?: string | string[]; // Type of node
}

Then, I commented out the test for schemaObject.format.

Then, I changed supportedJsDocTags to this:

  const supportedJsDocTags = [
    "description", "default", "example", 'pattern', 'format'
  ] as const;

Finally, I added the following:

  if ('nullable' in schemaObject) {
    output.push(`@nullable ${schemaObject.nullable ? 'true' : 'false'}`);
  }

These changes supported the tags I mentioned, and generated JSDoc tags that look correct to me.

Checklist

@robogeek robogeek added enhancement New feature or request openapi-ts Relevant to the openapi-typescript library labels Oct 21, 2024
@robogeek
Copy link
Author

This is a diff for the changes I made
diff.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-ts Relevant to the openapi-typescript library
Projects
Development

No branches or pull requests

2 participants