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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing description comment on referenced type #1

Open
hborchardt opened this issue May 9, 2023 · 0 comments
Open

Missing description comment on referenced type #1

hborchardt opened this issue May 9, 2023 · 0 comments

Comments

@hborchardt
Copy link

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

I am using typeconv to convert the following JSONSchema to TypeScript. Unfortunately the description is not converted into a comment when the type of a property is not a primitive but a reference to another property in the schema.

{
  "type" : "object",
  "additionalProperties" : false,
  "properties" : {
    "a" : {
      "$ref" : "#/definitions/A"
    }
  },
  "definitions" : {
    "B" : {
      "type" : "object",
      "additionalProperties" : false,
      "properties" : {
        "_id" : {
          "type" : "string"
        }
      },
      "required" : [ ]
    },
    "A" : {
      "type" : "object",
      "additionalProperties" : false,
      "properties" : {
        "_id" : {
          "description": "niice",
          "type" : "string"
        },
        "b" : {
          "description": "nice",
          "$ref" : "#/definitions/B"
        }
      },
      "required" : [ ]
    }
  }
}

Observed output after typeconv -f jsc -t ts a.json: (note the missing comment on property b)

export interface B {
    "_id"?: string;
}

export interface A {
    /** niice */
    "_id"?: string;
    b?: B;
}

Expected output:

export interface B {
    "_id"?: string;
}

export interface A {
    /** niice */
    "_id"?: string;
    /** nice */
    b?: B;
}

Here is the diff that solved my problem:

diff --git a/node_modules/core-types-json-schema/dist/lib/json-schema-to-core-types.js b/node_modules/core-types-json-schema/dist/lib/json-schema-to-core-types.js
index 8476985..9c78e86 100644
--- a/node_modules/core-types-json-schema/dist/lib/json-schema-to-core-types.js
+++ b/node_modules/core-types-json-schema/dist/lib/json-schema-to-core-types.js
@@ -244,9 +244,9 @@ function fromSchema(schema, ctx) {
     };
     if (schema.type === undefined) {
         if (schema.$ref)
-            return { ...makeRefType(schema.$ref), ...constEnum };
+            return annotate({ ...makeRefType(schema.$ref), ...constEnum }, schema);
         else
-            return { type: 'any', ...constEnum };
+            return annotate({ type: 'any', ...constEnum }, schema);
     }
     const types = ensureArray(schema.type)
         .map(type => fromSchemaAndType(schema, type, constEnum, ctx));

This issue body was partially generated by patch-package.

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

1 participant