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

Missing '"nullable": true' in resulting jsonschema #419

Open
oddball opened this issue May 6, 2021 · 7 comments
Open

Missing '"nullable": true' in resulting jsonschema #419

oddball opened this issue May 6, 2021 · 7 comments

Comments

@oddball
Copy link

oddball commented May 6, 2021

I am using https://www.graphql-code-generator.com/ to generate types from graphql schemas.
I am generating json schemas from those types, but fields that are nullable does not come out as nullable.

test.ts:

export type Maybe<T> = T | null;

export type Scalars = {
    String: string;
};

export type CreateTransactionInput = {
    readonly brokerTradeId?: Maybe<Scalars["String"]>;
};
> npx typescript-json-schema --noExtraProps test.ts CreateTransactionInput
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "additionalProperties": false,
    "properties": {
        "brokerTradeId": {
            "type": "string"
        }
    },
    "type": "object"
}

In my mind, the result is missing below on property brokerTradeId

"nullable": true,

What am I doing wrong?

@frontendphil
Copy link

frontendphil commented May 7, 2021

Came here for exactly that.

Some more of my context. I've defined a type:

type A = string | null

I know that when I set --strictNullCheck I get a type: ['string', 'null'] in the resulting schema. However, the typescript support of ajv complains that this is not valid JSON schema syntax and that the property should rather have nullable: true set.

So, I have no idea who is right on this one :D

@nicolabello
Copy link

nicolabello commented Jun 28, 2021

In my case, I'm using the programmatic version.

For:
export interface Data { name: string | null; }

When I execute:
TJS.generateSchema(program, 'Data', { ref: false, required: true, strictNullChecks: true });

I get:
{ 'type': 'object', 'properties': { 'name': { 'type': 'string' } }, 'required': ['name'], '$schema': 'http://json-schema.org/draft-07/schema#', }

It should be { 'type': ['string', 'null'] }

@CloudPower97
Copy link

I have the same exact problem as described by @frontendphil
Did you manage to resolve this issue somehow?

@sadnessOjisan
Copy link

I have same question about #419 (comment).
Is there workaround to solve it?

@fxalgrain
Copy link

You can manage it using validationKeywords option.
I'm using the programmatic version too. So I set:

  await TJS.exec(`${projectFolder}/tsconfig.json`, "*", {
    ...TJS.getDefaultArgs(),
    required: true,
    noExtraProps: true,
    defaultNumberType: "integer",
    include: ["**/*.dto.ts"],
    validationKeywords: ["example", "nullable"],
    out,
  });

I get in my output :

"title": {
  "description": "title of the item.",
  "example": "'My title'",
 "nullable": true,
 "type": "string"    
},

@wma-dev
Copy link

wma-dev commented Feb 7, 2023

I know that when I set --strictNullCheck I get a type: ['string', 'null'] in the resulting schema. However, the typescript support of ajv complains that this is not valid JSON schema syntax and that the property should rather have nullable: true set.

This works, but it is --strictNullChecks (with an s) now

jwatzman added a commit to getcord/sdk-js that referenced this issue Jun 14, 2023
This is apparently needed for TypeScript to know anything at all about
`| null`, so that typescript-json-schema can pick that up and add it to
the right place in the generated schemas.

See also: YousefED/typescript-json-schema#419


Test Plan: Looked at generated output, seems reasonable. Tests pass.

Reviewers: jozef-mokry, km-nur, flooey

Reviewed By: flooey

Pull Request: getcord/monorepo#4998

monorepo-commit: a50605311aba8e93c1abb8174b6e378b71f9bddf
@Wavewash
Copy link

Wavewash commented Feb 6, 2024

Ran into this issue as well - we had a project that couldn't have strict mode on; Adding the work around that worked for us:

The strictNullChecks option isn't respected unless you also have "strict: true" set in your tsconfig also. If you can not have "strict: true" set in your tsconfig (or on by default) you can force the null value to show in the schema by adding the @nullable annotation to your property:

export interface KeyframeData {
    /**
     * @nullable true
     */
    draglock?: boolean;
}

produces:

KeyframeData": {
            "properties": {
                "draglock": {
                    "type": [
                        "boolean",
                        "null"
                    ]
                }
            }
            "type": "object"
        },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants