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

postTransform would be more useful with schema information #2013

Open
1 task done
duncanbeevers opened this issue Nov 20, 2024 · 2 comments · May be fixed by #2049
Open
1 task done

postTransform would be more useful with schema information #2013

duncanbeevers opened this issue Nov 20, 2024 · 2 comments · May be fixed by #2049
Assignees
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@duncanbeevers
Copy link
Contributor

duncanbeevers commented Nov 20, 2024

Description

The postTransform option is handed the ts types as they're generated from each OpenAPI schema member in transformSchemaObject.
However, although there is a 1:1 mapping between calls to the postTransform, and OpenAPI schema members, postTransform has no access to the OpenAPI schema object associated with the generated ts node.

I think postTransform would be more useful if it gave access to the schema node, and it should be added to the options argument, passed to postTransform, along with path and ctx.

Checklist

@duncanbeevers duncanbeevers added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Nov 20, 2024
@duncanbeevers
Copy link
Contributor Author

I was able to reach the schema node a bit indirectly. Rather than change the postTransform signature or modify the TransformNodeOptions, I reached the schema node by resolving the path, already present on the TransformNodeOptions.

postTransform(type: ts.TypeNode, options: openapiTS.TransformNodeOptions): ts.TypeNode {
  const schema = options.path ?
    options.ctx.resolve<openapiTS.ReferenceObject | openapiTS.SchemaObject>(options.path) :
    undefined;

  if (
    schema &&
    !("$ref" in schema) &&
    Object.hasOwn(schema, "x-my-custom-property")
  ) {
    return ts.factory.createUnionTypeNode();
  }

  return type;
}

It was still a bit of a chore to discover this path, but since all the necessary pieces seem to be present, perhaps this issue should be closed.

@drwpow
Copy link
Contributor

drwpow commented Nov 22, 2024

Ha that’s clever! But agreed we could make this easier. We’d accept adding anything onto options (e.g. options.schema). Thanks for suggesting!

@drwpow drwpow self-assigned this Nov 27, 2024
duncanbeevers added a commit to duncanbeevers/openapi-typescript that referenced this issue Dec 13, 2024
@duncanbeevers duncanbeevers linked a pull request Dec 13, 2024 that will close this issue
6 tasks
duncanbeevers added a commit to duncanbeevers/openapi-typescript that referenced this issue Dec 13, 2024
duncanbeevers added a commit to duncanbeevers/openapi-typescript that referenced this issue Dec 13, 2024
duncanbeevers added a commit to duncanbeevers/openapi-typescript that referenced this issue Dec 13, 2024
duncanbeevers added a commit to duncanbeevers/openapi-typescript that referenced this issue Dec 13, 2024
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
Development

Successfully merging a pull request may close this issue.

2 participants