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

Error when converting schema with if / then #27

Closed
nathalie opened this issue Apr 28, 2022 · 6 comments
Closed

Error when converting schema with if / then #27

nathalie opened this issue Apr 28, 2022 · 6 comments

Comments

@nathalie
Copy link

nathalie commented Apr 28, 2022

Hi, I get an error when I try to convert the schema attached

{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": ["css", "js", "i18n", "json"]
    },
    "locale": {
      "type": "string"
    }
  },
  "if": {
    "properties": {
      "type": {
        "const": "i18n"
      }
    }
  },
  "then": {
    "required": ["locale"]
  }
}

TypeError: Cannot convert undefined or null to object
at stripIllegalKeywords (/home/nathalie/projects/focus/test-open-api/node_modules/@openapi-contrib/json-schema-to-openapi-schema/index.js:48:2)
at convertSchema (/home/nathalie/projects/focus/test-open-api/node_modules/@openapi-contrib/json-schema-to-openapi-schema/index.js:55:11)
at _apply (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:270:14)
at Object._processArrayOfSchemas (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:173:5)
at _processSchemaKeyword (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:146:24)
at subschemaWalk (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:102:7)
at _apply (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:283:3)
at Object._processArrayOfSchemas (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:173:5)
at _processSchemaKeyword (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:146:24)
at subschemaWalk (/home/nathalie/projects/focus/test-open-api/node_modules/@cloudflare/json-schema-walker/lib/schemaWalk.js:102:7)

@jonluca
Copy link
Member

jonluca commented Aug 1, 2022

This is fixed in this PR #29

It is also deployed on npm as browser-json-schema-to-openapi-schema, but hopefully the PR will be accepted and we can cut a release for this package soon

@nathalie
Copy link
Author

nathalie commented Aug 8, 2022

Hi Jonluca, is it possible that with the latest version, I can no longer reference a definition inside another one? I saw you also added support for dereferencing definitions, so possibly this is a related side effect? Or is it intended?

@jonluca
Copy link
Member

jonluca commented Aug 8, 2022

Do you have an example?

@nathalie
Copy link
Author

nathalie commented Aug 8, 2022

{
  "definitions": {
    "envVarName": {
      "type": "string",
      "pattern": "^[A-Z_]+[A-Z0-9_]*$"
    },
    "configvariable": {
      "type": "object",
      "properties": {
        "name": { "$ref": "#/definitions/envVarName" },
        "default": { "type": "string" },
        "required": { "type": "boolean", "default": true }
      },
      "required": ["name"],
      "additionalProperties": false
    }
  },
  "$schema": "https://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/root.json",
  "type": "object",
  "title": "Component Manifest Schema",
  "required": [
    "componentId"
  ],
  "additionalProperties": false,
  "properties": {
    "componentId": {
      "$id": "#/properties/componentId",
        "type": "string",
        "title": "The component id Schema",
        "pattern": "^(.*)$"
    },
    "configurationTemplate": {
      "$id": "#/properties/configurationTemplate",
        "type": "array",
        "title": "The Configurationtemplate Schema",
        "items": {
        "$ref": "#/definitions/configvariable"
      }
    }
  }
}

This schema did not throw an error with version 2.1.1. After upgrading it throws
MissingPointerError: Token "definitions" does not exist.

I can work around it by adding the envVarName definition inside the definition of the configVariable, but that means I need to duplicate this part.

@jonluca
Copy link
Member

jonluca commented Aug 8, 2022

Ah yes, looks like this is an issue with the new definitions crawler.

I fixed it in this PR, but for now you can set options.convertUnreferencedDefinitions = false to get around it.
#35

@nathalie
Copy link
Author

nathalie commented Aug 8, 2022

That works! Thanks a lot for fixing this.

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

3 participants