grantila / grantila/core-types-json-schema
Missing description comment on referenced type
- Dominant language
- TypeScript
- Stars
- 5
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
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.
```json
{
"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`)
```ts
export interface B {
"_id"?: string;
}
export interface A {
/** niice */
"_id"?: string;
b?: B;
}
```
Expected output:
```ts
export interface B {
"_id"?: string;
}
export interface A {
/** niice */
"_id"?: string;
/** nice */
b?: B;
}
```
Here is the diff that solved my problem:
```diff
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](https://github.com/ds300/patch-package/issues/296).
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the issue with `typeconv -f jsc -t ts a.json`, then inspect `node_modules/core-types-json-schema/dist/lib/json-schema-to-core-types.js`, especially `fromSchema` and the `$ref` branch. Compare the generated output with the expected comments for property `b`; done means the description is preserved for referenced properties without regressing primitive properties.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 40/100