-
-
Notifications
You must be signed in to change notification settings - Fork 528
anyOf causes "| undefined | undefined" in v7 #1441
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
I believe this is working as expected. A schema object with If you copied & pasted the entire I know this is a contrived example but hopefully this applies to the original, full schema as well. |
Take a look at the example in the json-schema docs https://json-schema.org/understanding-json-schema/reference/conditionals#implication And I'm not actually in control of the spec I'm using, it's a US Department of Transportation spec: https://github.com/usdot-jpo-ode/wzdx/blob/main/schemas/4.2/WorkZoneFeed.json |
Yeah you’re right that is a strange example I haven’t seen before. However, from the technical specification:
While I agree that specifying partial schemas like this would be nice, I think the biggest problem it would introduce is nondeterministic types. In your original example, we could expect the types to generate those results only because it was declared inline. But suppose it was a Though I think this library is possibly missing some more advanced JSON Schema composition features (like (edit: clarity) |
In the meantime, as a temporary workaround, you can use the transform Node.js API to override type generation on individual nodes. Which is ideal for schemas you can’t control. |
I'll take a look at the transform API, that could make it work for me, thanks. |
The transform function requires doesn't allow the user to return a JSON schema that then gets passed to I need access to the bundled schema anyway in order to to generate some On the spec side, either
or
If it turns out it's valid, I think openapi-typescript should just mark both fields as optional and not do Is there anything I can do to help figure out if it is valid? |
This seems like a good strategy to go with overall. openapi-typescript’s transform API is meant to be somewhat lightweight and allow minor adjustments. But a Redocly custom plugin is the better way to perform sweeping, larger schema changes and have full control.
I can’t point to a specific one-liner in the spec that expressly forbids this. But from my understanding, the required field can only exist on Or, put another way, I think of OpenAPI validation in 2 levels: syntactically-valid vs logically-valid. The former is just simply “does it match the expected syntax?” and this is a very simple check that the schema is parseable. And in that sense, there may not be an explicit error if |
The two levels make a lot of sense, I even found that mentioned in the docs, though that page is nothing but examples of not putting full schemas with properties in I the schema for Thing validates against the a JSON schema meta schema, so I tried just testing it and it seems to work as I expected in a few online JSON schema validators? Your interpretation makes the most sense from reading the spec, but the docs and testing seems to suggest otherwise? |
I'm not sure if I should create another ticket, but I think it is related to this thread. My schema: {
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "demo thing",
"description": "demo thing"
},
"servers": [{ "url": "whatever" }],
"paths": {
"/foo": {
"get": {
"operationId": "GetFoo",
"responses": {
"200": {
"description": "test",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/FooDTO"
}
}
}
}
}
}
}
},
"tags": [],
"components": {
"schemas": {
"FooDTO": {
"type": "object",
"properties": {
"variables": {
"type": "object",
"additionalProperties": {
"oneOf": [{ "type": "array", "items": { "type": "string" } }, { "type": "string" }]
}
}
}
}
}
}
} Version schemas: {
FooDTO: {
variables?: {
[key: string]: string[] | string;
};
};
}; Version schemas: {
FooDTO: {
variables?: {
[key: string]: (string[] | string) | undefined;
};
};
}; |
I went on the json schema slack to ask for clarification, and was told that the
|
That’s an intentional breaking change in 7.x (for now). There have been many discussions in this project around how to handle Ultimately, the goal is to mark |
Thank you @drwpow for detailed explanation, I believe it is very complex topic. Is there any other way to achieve output type that I need using JSON schema/OpenAPI?
|
This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed. |
This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem. |
Description
If you use
anyOf
withrequired
, the resulting interface has$expected | unknown | unknown
.Sample file:
sample output
openapi-typescript
7.0.0-next.2
v18.17.1
debian 11 bullseye
Reproduction
npx openapi-typescript swagger.json -o output.ts
Expected result
Checklist
npx @redocly/cli@latest lint --lint-config error --skip-rule security-defined --skip-rule operation-4xx-response --skip-rule info-license --skip-rule operation-summary swagger.json
)The text was updated successfully, but these errors were encountered: