glideapps / glideapps/quicktype

Wrong enum name when using union with null

Open
#2,410 2 comments 2 reactions 0 assignees View on GitHub
bug input:JSON Schema naming
Dominant language
TypeScript
Stars
13.9k
Forks
1.2k
Avg merge
8h 53m
Merged PRs (30d)
369

Description

I've an issue when trying to convert a JSON schema file to Typescript types. The JSON schema comes from a [Rust models](https://github.com/okp4/contracts/blob/f2afc6349a654cb6ffa1771cb0f750bd68f0b7c1/contracts/okp4-cognitarium/src/msg.rs#L110) that has been converted to [JSON Schema](https://github.com/okp4/okp4-contract-schema/blob/30b9686d9f946563d8ca60c53df388ee75186ec7/schema/okp4-cognitarium/query.json#L55-L66).

Given this schema : (I've simplified the schema for this exemple)

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ConstructResponse",
"description": "Represents the response of a [QueryMsg::Construct] query.",
"type": "object",
"required": [
"format"
],
"properties": {
"format": {
"description": "The format of the data.",
"anyOf": [
{
"$ref": "#/definitions/DataFormat"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false,
"definitions": {
"DataFormat": {
"title": "DataFormat",
"description": [...],
"oneOf": [
{
"title": "Turtle",
"description": [...]
"type": "string",
"enum": [
"turtle"
]
},
{
"title": "RDF XML",
"description": [...],
"type": "string",
"enum": [
"rdf_xml"
]
},
{
"title": "N-Triples",
[...]
},
{
"title": "N-Quads",
[...]
}
]
}
}
}
```

And running QuickType to convert this json schema in Typescript types,

```bash
quicktype -s schema schema.json -o test.ts --prefer-type
```

The conversion use the first enum case as the name of the enum, here the enum will be named `Turtle` instead of `DataFormat`.

```ts
/**
* Represents the response of a [QueryMsg::Construct] query.
*/
export type Test = {
format: Turtle | null;
}

export enum Turtle {
NQuads = "n_quads",
NTriples = "n_triples",
RDFXML = "rdf_xml",
Turtle = "turtle",
}
```

Expected result

```ts
/**
* Represents the response of a [QueryMsg::Construct] query.
*/
export type Test = {
format: DataFormat | null;
}

export enum DataFormat {
NQuads = "n_quads",
NTriples = "n_triples",
RDFXML = "rdf_xml",
Turtle = "turtle",
}
```

When I remove `{ "type": "null" }` from the `anyOf` property of `DataFormat`, the enum is named correctly.

JSON without the `null` union

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ConstructResponse",
"description": "Represents the response of a [QueryMsg::Construct] query.",
"type": "object",
"required": [
"format"
],
"properties": {
"format": {
"description": "The format of the data.",
"anyOf": [
{
"$ref": "#/definitions/DataFormat"
}
]
}
},
"additionalProperties": false,
"definitions": {
"DataFormat": {
"title": "DataFormat",
"description": [...],
"oneOf": [
{
"title": "Turtle",
"description": [...]
"type": "string",
"enum": [
"turtle"
]
},
{
"title": "RDF XML",
"description": [...],
"type": "string",
"enum": [
"rdf_xml"
]
},
{
"title": "N-Triples",
[...]
},
{
"title": "N-Quads",
[...]
}
]
}
}
}
```

Give a good enum name but is not what I want because DataFormat is optional :
```ts
/**
* Represents the response of a [QueryMsg::Construct] query.
*/
export type Test = {
format: DataFormat;
}

export enum DataFormat {
NQuads = "n_quads",
NTriples = "n_triples",
RDFXML = "rdf_xml",
Turtle = "turtle",
}
```

This null type union has been added because `format` property can be `null` since this attribute is optional in rust.

I hope you can help me, I'm wondering if it's an issue in this library for this particular case or if it's the JSON Schema that has been malformed by the rust library.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.