-
-
Notifications
You must be signed in to change notification settings - Fork 469
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
required fields generated as optional #1467
Comments
Hey there, I'm not sure if this is a bug. Please correct me if I'm misunderstanding. Looking at the yaml provided: AaSequenceBaseRequestForCreate:
allOf:
- $ref: '#/components/schemas/AaSequenceBaseRequest'
- required:
- aminoAcids
- name I'm not sure if this is doing what you want it to. It looks like you're trying to extend This might do the trick without using # Assuming the schema you're referencing looks something like this:
AaSequenceBaseRequest:
type: object
properties:
- name:
type: string
- folderId:
type: string
# ...
# You should be able to reference the schema without `allOf` like this:
AaSequenceBaseRequestForCreate:
$ref: '#/components/schemas/AaSequenceBaseRequest'
required:
- aminoAcids
- name Hopefully this solves your problem! Here's some further readings if you'd like a detailed explanation: https://redocly.com/docs/resources/all-of/ If not, could you please provide more of your yaml schema, including the definition of |
Thanks, this seems like it probably the cause. I didn't notice the required items were in the array. Unfortunately, I am unable to test this because I keep getting a new error that it can't find my yaml file, even though it is actually there.
but the file is there! I can't figure out what is wrong. Anyway, we can close this as it does make a lot of sense what you are suggesting. I just can't verify on my end for literally no discernable reason that I can find. Additional Info if you have time and desire:file is there
This is the openapi file I'm using from https://dzone.com/articles/a-sample-openapi-30-file-to-get-started
|
OK, I updated the npm version to [email protected] and now the error is gone (the spec file is found) I made the changes you suggested but, unfortunately, the fields are still optional in the generated types file.
|
There’s currently not a way in openapi-typescript to use composition ( There’s also not a way to combine This may be more a limitation of this library and TypeScript rather than OpenAPI as-designed. The requirement to declare full, complete schema objects is to produce deterministic types. In your case, I’d recommend something like: AaSequenceBaseRequestForCreate:
allOf:
- $ref: '#/components/schemas/AaSequenceBaseRequest'
type: object
required:
- aminoAcids
- name
properties:
animoAcids:
$ref: '#/components/schemas/AminoAcids'
name:
type: string Note that you can always pull out individual properties into their own components for better reuse. |
Thanks @drwpow The response suggested by @davidleger95 eliminated composition (I removed the
When you say "There’s also not a way to combine In the
Is this why a $ref can't be modified? I'm interested in contributing to the library to allow modification of required fields from referenced schemas, possibly including with oneOf/allOf/anyOf compositions, by returning WithRequired<> wrappers around referenced schemas. Are you interested? |
This comment was marked as resolved.
This comment was marked as resolved.
I've discovered the cause of this, at least for my use case. My openapi spec is based off 3.0.1, and it uses the "nullable", which this package seems to ignore as it's marked as deprecated because it's not a concept in the 3.1 spec (presumably, I didn't dig into it more) This is the patch, which I didn't open a PR as there's probably other considerations.
|
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
I generated types from https://benchling.com/api/v2/openapi.yaml file, and all of the required attributes are shown as optional in typescript.
openapi-typescript
6.7.1
20.10.0
Windows 11
Reproduction
I created a react app with typescript
$ npx create-react-app bugapp --template typescript
I added "noUncheckedIndexedAccess":true" to the react-generated tsconfig.json file, as suggested by the docs
I copy pasted the openapi specs from https://[benchling.com/api/v2/openapi.yaml] (https://benchling.com/api/v2/openapi.yaml) and pasted them into a file bugapp/openapi.yaml
I created a file bugapp/api.d.ts
then I ran command:
$ npx openapi-typescript ./openapi.yaml -o ./api.d.t
The resulting file incorrectly indicates required fields as optional.
One example:
//yaml
AaSequenceBaseRequestForCreate:
allOf:
- $ref: '#/components/schemas/AaSequenceBaseRequest'
- required:
- aminoAcids
- name
// types
ends up pointing to an object where these are not required.
aminoAcids?: string;
name?: string
...real result below
Expected result
I expected the name and aminoAcids attributes to be required. But it looks like they are optional because the types point to name?, and aminoAcids?
Checklist
It did NOT pass, but all the errors are due either to lack of 4XX response, and "spec" errors that dislike having some nullable schema properties, but none of them are not the same properties that I am talking about. The errors stem from 'nullable: true' statements on properties that also indicate 'allOf', or 'anyOf', of 'oneOf' - which is known to be wrong in 3.0 (https://dev.to/danieltott/openapi-30-how-to-make-objects-nullable-that-use-oneof-allof-or-anyof-55on) but these are unrelated to the properties I am concerned with. I am having an issue with '-required: true' statement not being noticed.
npx @redocly/cli@latest lint
)The text was updated successfully, but these errors were encountered: