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 $id prefix in $ref #1732

Open
wizardpisces opened this issue Jul 26, 2023 · 3 comments
Open

Missing $id prefix in $ref #1732

wizardpisces opened this issue Jul 26, 2023 · 3 comments

Comments

@wizardpisces
Copy link

wizardpisces commented Jul 26, 2023

How to produce

ts-json-schema-generator -f './tsconfig.json' -p './type.ts' -o './scheme.json' --id='api'

./type.ts


export type ApiSchema = {
    N: B
}

export type B = string

What to expect?

./schema.json


{
  "$id": "api",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "ApiSchema": {
      "type": "object",
      "properties": {
        "N": {
          "$ref": "api#/definitions/B" // with api prefix
        }
      },
      "required": [
        "N"
      ],
      "additionalProperties": false
    },
    "B": {
      "type": "string"
    }
  }
}

What is actually happening?

./schema.json

{
  "$id": "api",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "ApiSchema": {
      "type": "object",
      "properties": {
        "N": {
          "$ref": "#/definitions/B" // missing api prefix
        }
      },
      "required": [
        "N"
      ],
      "additionalProperties": false
    },
    "B": {
      "type": "string"
    }
  }
}

Is this by design?

I think it's a serious bug, because it sabotages the combination with ajv validation

By the way typescript-json-schema can generate the expected result

@wizardpisces wizardpisces changed the title Missing $id Info in $ref Missing $id prefix in $ref Jul 26, 2023
@domoritz
Copy link
Member

Huh, can you look into why ID is being ignored?

@wizardpisces
Copy link
Author

Huh, can you look into why ID is being ignored?

No idea why it's designed like this, but I searched source code and got below discover

In ts-json-schema-generator, DefinitionTypeFormatter and ReferenceTypeFormatter hard coded '#/definition' as prefix
image

While in typescript-json-schema, $id will be used as prefix
image

Since ts-json-schema-generator is an extended version of typescript-json-schema, so I think their basic behavior should be same?

@domoritz
Copy link
Member

Yeah, looks like we accidentally broke that at some point. Let's fix it.

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

2 participants