-
-
Notifications
You must be signed in to change notification settings - Fork 528
v7 Preview 🎉 #1368
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
Comments
Great to hear that I did quick test with our project and ran into a typing issue. When extending a model using
... the generated code does not compile: Our backend colleagues use the same OpenAPI file using the openapi-generator kotlin-spring plugin, so it seems to a be valid spec. Here's an example repo to reproduce the issue: https://github.com/luchsamapparat/openapi-typescript-7-bug |
@luchsamapparat the generated code seems to be working correctly; the error seems to be coming from the fact your schema specifies |
If you have another look at my example, the You can copy and paste the full example OpenAPI YAML from my repo into the Swagger Editor which parses it correctly: |
Would be nice to support UPD: |
Thanks to @joostme who pointed out that there's a workaround for my problem: |
running with see: npx openapi-typescript@next https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml --immutable | head error:
|
@stefanprobst good catch 🙏! Will add some immutable tests to make sure the types are valid. Sidenote: I did learn through the v7 rewrite that TypeScript’s AST does let you generate invalid TS without stopping you. Which is fun 🙃 |
Upgraded to v7 without issues. Generated file looks neat and tidy - especially when using external file refs. Thank you for this awesome job! |
@drwpow may it be the case that there were some changes in handling of the Input: CityLinks:
type: object
required: [socials]
properties:
socials:
type: object
additionalProperties:
type: string Output CityLinks: {
socials: {
// There was no `| undefined` in later v6 versions
[key: string]: string | undefined;
};
}; |
Great question! That actually has to do with how TS handles discriminated unions within other tools like openapi-fetch. For example, In v6, comparing the response objects between 2 or more endpoints would yield a very complex union because each would have different combinations of request methods—often each would be a completely different permutation of keys. And to TS, comparing objects with different keys are “apples to oranges” even if we don’t intend for them to be. But when we add So it not only helps TS do its job better, and makes more advanced utilities built off openapi-typescript better, but I think the |
Gotcha, this is super helpful to know, thanks for the link to the reference article too! does this result also in a speed up in the auto complete type hints as a result? |
hey i think i have found an issue with v7. i've dug into it and i have found the source of the issue... a number of my endpoints can return a so the generated types look like this: responses: {
201: {
headers: {
[name: string]: unknown;
};
content: {
// ...
};
};
204: {
headers: {
[name: string]: unknown;
};
content?: never; // <-- this is the source of the issue
};
// ...
}; the potentially undefined Here's a typescript playground which contains an abridged version of my generated schema and some code at the bottom to demonstrate the issue. if you remove the I do not have this issue with Edit: for completeness, the 204: {
content: never;
}; |
Another issue sorry! In requestBody:
content:
application/vnd.api+json:
schema:
required:
- data
properties:
data:
type: object
required:
- type
additionalProperties: false
properties:
type:
type: string
id:
type: string
attributes:
type: object
properties:
first_name:
type: string
maxLength: 100
last_name:
type: string
maxLength: 100
email:
type: string
format: email
maxLength: 100
language:
type: string
nullable: true
default: en
maxLength: 20
required:
- email
- initials this will generate a type like: requestBody?: {
content: {
"application/vnd.api+json": {
data: {
type: string;
id?: string;
attributes?: {
first_name?: string;
last_name?: string;
/** Format: email */
email: string;
/** @default en */
language: string | null; // <-- ❌ not optional?
};
};
};
};
}; i believe this is incorrect, given the openapi spec on
in requestBody?: {
content: {
"application/vnd.api+json": {
data: {
type: string;
id?: string;
attributes?: {
first_name?: string;
last_name?: string;
/** Format: email */
email: string;
/** @default en */
language?: string | null;
};
};
};
};
}; here's a stackblitz demonstrating the issue. it will output to |
@WickyNilliams thanks for flagging! I will investigate that specifically. There are other breaking changes I’m tracking that will require a breaking version change for openapi-fetch (but fortunately the breaking change is less code since 7.x makes inference simpler/safer). Right now it’s known openapi-fetch only works with 6.x, and an upcoming breaking version of openapi-fetch will be released for 7.x (as a fast follow) |
@WickyNilliams also for the |
is this mentioned in the docs? i looked in multiple before trying out v7, and since i didn't spot anything i assumed that they were compatible. might be worth sticking a big banner somewhere :D or perhaps list i have pinned to v6 in the meanwhile. glad to hear v7 enables some simplification of the fetcher!
ah i see that in the full changelog above now. the docs site itself still says it defaults to |
A note on the docs would be a good idea! Will add that.
Mostly from this issue, and if you search you’ll find other instances where it seemed like more users were expecting this behavior than not. And it seems like the spec suggests this should be the default behavior? Open to feedback if I’m misunderstanding something; I had just flagged this as a deviation from the specification (and with a spec deviation + multiple issues raised about the default behavior seems like a good hint to change it). |
in my reading i think there is a sutble difference between inputs and outputs. for outputs/responses, the set of non-optional fields should be the union of reading the linked issue, i think that's what they're asking also. see the second bullet point (emphasis theirs):
i found this related issue #584 which i think is talking about default values in responses being required. there's also this comment which says:
|
Ah thank you that is a really great distinction. Will also add that to the test suite. That may mean some nuances of |
For sure! Please post back here as and when something changes |
@ogjonixyz Thank you! Will track that as well. Appreciate all the testing and reporting issues while it’s still in beta. |
Hello. Got some issue.
|
Ah it was unpinned by mistake, but hopefully can resume getting the final tests in and releasing an RFC sometime soon. That said, new issues are probably preferred, only for the reason to keep the notifications down for the people that commented here but weren’t expecting to see bug reports 🙂 |
If you need a beautiful and easy-to-use openapi ui document, you can take a look at this openapi-ui |
There are some great features here, specifically --enum. I can't get this working with Redocly in @7.0.0-next.5 when using the resolve headers for basic auth
I stepped through the Redocly code and there are multiple resolvers that make requests for the same file, kind of weird, one (or more) resolver instances isn't passed the config so it makes a unauthorized request and bombs the build. I am not creating an issue since I don't know how to isolate this to openapi-typescript's use of Redocly or Redocly itself, just thought I would note somewhere since I'm not finding any search results. Aside from this, I really don't want validation from Redoc forced in the same command where I generate types. I'm not sure why these operations are being combined. |
FWIW, I couldn't get v7 to work when I last tried due to a problem with Redocly. If I manually commented out some code in redocly itself, everything worked and I got types that were identical to v6. Well, nearly identical - it's not clear if my change to redocly was the reason the types changed or not. |
Did you ever try just passing the contents as a string? I'm thinking of doing that. Redoc wants me to have a env var with an entire base64 encoded username/password prepared just to use Basic Auth. So I need to script before I call openapi-typescript to assemble such a thing from an actual token, inject as a env var. It's not very palatable even if it was working. |
I didn't, is this what you mean? import fs from 'node:fs';
import openapiTS from 'openapi-typescript';
const schema = fs.readFileSync('openapi.json', 'utf8');
async function main() {
const types = await openapiTS(schema);
fs.writeFileSync('schema.ts', types);
}
main(); That gives me the same error as before from redocly: |
Yep that's what I meant. For me the problem is just when retrieving the spec via URL... But if I don't retrieve it this way I don't think I can use any of the other redoc features? All I want from v7 is --enum, might look at a custom transformer for now. |
Thank you for the great project @drwpow ! When I run {
"openapi": "3.1.0",
"info": {
"title": "Minimal API with Nullable Enum",
"version": "1.0.0"
},
"paths": {
"/example": {
"get": {
"summary": "Get Example Value",
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"value": {
"type": "string",
"enum": ["option1", "option2", "option3"],
"nullable": true
}
}
}
}
}
}
}
}
}
}
} For me value should be nullable: Is it a bug ? |
@gduliscouet-ubitransport yes that’s a bug! |
Testing the 7.0.0-next.8, with the export readonly enum OrderStatus { // TS1024: readonly modifier can only appear on a property declaration or index signature.
placed = "placed",
approved = "approved",
delivered = "delivered"
} But enum can not have the |
@JeanRemiDelteil great catch! Would love a PR to fix this (no need to open a separate issue) |
I will see what I can do with the time I have at hand ! (Ok, let's discuss more in the PR if there is the need to.) |
Another issue with v7 compared to v6 seems to be the handling of the --immutable flag for arrays. with v7 I got:
where in v6 it gives:
Is it by design ? |
Nope! Not intentional. Fixing this bug now, and going through all the reported issues so far to work toward a stable release soon. |
Upgraded to v7 on a pretty complex project with several openAPI definition involved without a single type error 👍 Thanks for the great job! |
Thanks everyone for your patience! 7.x has a release candidate up at This one took a little longer to bake than normal, but is also one of the biggest releases of this library when you factor in a complete AST rewrite, addition of schema validation via Redocly (along with a ton of functionality/features like Now the only thing blocking a So look forward to openapi-typescript |
👋 Hello! Posted a project update in discussions (don’t worry, it’s good) that people subscribing to this issue may be interested in. Would love thoughts from everyone here! TL;DR this project is getting sponsorship, $ will be going to contributors, and I’m not stepping down/away but would still like to invite others to contribute more and will probably move the project to a shared org to make that possible. |
What is the status of the extra Seems to still be an issue with |
v7 Preview
openapi-typescript@7
is now in preview! 🎉 The major additions to this library are:redocly.yaml
for API config (define your schemas there in one place, and useopenapi-typescript
to generate TypeScript without having to manage a separate config)--enum
flag for generating true TypeScript enums instead of unions (default behavior still unions)Full Changelog
Features
✨ Feature: automatically validate schemas with Redocly CLI (docs). No more need for external tools to report errors! 🎉
✨ Feature: bundle schemas with Redocly CLI
✨ Feature: add
enum
option to export top-level enums from schemas ([Feature request] Generate Enums AND Union types #941)✨ Feature: add
formatOptions
to allow formatting TS output✨ Feature: header responses add
[key: string]: unknown
index type to allow for additional untyped headers✨ Feature: allow configuration of schemas via
apis
key in redocly.config.yaml. See docs for more info.Breaking Changes
transform()
andpostTransform()
. To migrate, you’ll have to use thetypescript
compiler API:Though it’s more verbose, it’s also more powerful, as now you have access to additional properties of the generated code you didn’t before (such as injecting comments).
For example syntax, search this codebae to see how the TypeScript AST is used.
Also see AST Explorer’s
typescript
parser to inspect how TypeScript is interpreted as an AST.--auth
,--httpHeaders
,--httpMethod
, andfetch
(Node.js-only) options were all removed from the CLI and Node.js API--immutable-types
has been renamed to--immutable
--support-array-length
has been renamed to--array-length
:never
. This includes keys of the Components Object as well as HTTP methods.external
export in schemas anymore. Everything gets flattened into thecomponents
object instead (if referencing a schema object from a remote partial, note it may have had a minor name change to avoid conflict).defaultNonNullable
option now defaults totrue
. You’ll now need to manually setfalse
to return to old behavior.redocly.yaml
config. Specify multiple schemas with outputs in there instead. See Multiple schemas for more info.And dozens of small performance improvements and quality fixes.
Give it a try today with the
@next
flag:Testers wanted 🙏
Will pin this issue for a while while people kick the tires and report bugs while a release candidate is developed. For many people, it should be a drop-in replacement; for others, it will only require a few minor changes.
If people could report any errors/issues they face while trying this, that’d be much appreciated. Any issues posted here or as separate issues will help patch holes in the docs site.
The text was updated successfully, but these errors were encountered: