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

feat: uri encoded $refs (#617) #618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,13 @@ export type MyTuple = [string, number, boolean?];
```


## [type-aliases-tuple-with-names](./test/programs/type-aliases-tuple-with-names)

```ts
export type MyTuple = [a: string, b: 123, c?: boolean, ...d: number[]];
```


## [type-aliases-tuple-with-rest-element](./test/programs/type-aliases-tuple-with-rest-element)

```ts
Expand Down
4 changes: 2 additions & 2 deletions test/programs/generic-anonymous/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
},
"properties": {
"value1": {
"$ref": "#/definitions/MyGeneric<string,number>"
"$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E"
},
"value2": {
"$ref": "#/definitions/MyGeneric<number,string>"
"$ref": "#/definitions/MyGeneric%3Cnumber%2Cstring%3E"
}
},
"required": [
Expand Down
4 changes: 2 additions & 2 deletions test/programs/generic-multiargs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
},
"properties": {
"value1": {
"$ref": "#/definitions/MyGeneric<string,number>"
"$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E"
},
"value2": {
"$ref": "#/definitions/MyGeneric<number,string>"
"$ref": "#/definitions/MyGeneric%3Cnumber%2Cstring%3E"
}
},
"required": [
Expand Down
4 changes: 2 additions & 2 deletions test/programs/generic-multiple/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
},
"properties": {
"value1": {
"$ref": "#/definitions/MyGeneric<number>"
"$ref": "#/definitions/MyGeneric%3Cnumber%3E"
},
"value2": {
"$ref": "#/definitions/MyGeneric<string>"
"$ref": "#/definitions/MyGeneric%3Cstring%3E"
}
},
"required": [
Expand Down
6 changes: 3 additions & 3 deletions test/programs/generic-recursive/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"additionalProperties": false,
"properties": {
"field": {
"$ref": "#/definitions/MyGeneric<string,number>"
"$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E"
}
},
"required": [
Expand All @@ -18,7 +18,7 @@
"additionalProperties": false,
"properties": {
"field": {
"$ref": "#/definitions/MyGeneric<number,string>"
"$ref": "#/definitions/MyGeneric%3Cnumber%2Cstring%3E"
}
},
"required": [
Expand All @@ -30,7 +30,7 @@
"additionalProperties": false,
"properties": {
"value": {
"$ref": "#/definitions/MyGeneric<string,number>"
"$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E"
}
},
"required": [
Expand Down
2 changes: 1 addition & 1 deletion test/programs/generic-simple/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"properties": {
"value": {
"$ref": "#/definitions/MyGeneric<number>"
"$ref": "#/definitions/MyGeneric%3Cnumber%3E"
}
},
"required": [
Expand Down
20 changes: 18 additions & 2 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ export class JsonSchemaGenerator {
// We don't return the full definition, but we put it into
// reffedDefinitions below.
returnedDefinition = {
$ref: `${this.args.id}#/definitions/` + fullTypeName,
$ref: createRefURI(this.args.id, fullTypeName),
};
}

Expand Down Expand Up @@ -1519,7 +1519,7 @@ export class JsonSchemaGenerator {
}, {});

returnedDefinition = {
$ref: `${this.args.id}#/definitions/` + fullTypeName,
$ref: createRefURI(this.args.id, fullTypeName),
...annotations,
};
}
Expand Down Expand Up @@ -1640,6 +1640,22 @@ export class JsonSchemaGenerator {
}
}

/**
* Generates the reference URI to the definition in the schema `id`
* containing the definition `name`.
*
* Encodes the `name` using `encodeURIComponent`.
*
* @see https://datatracker.ietf.org/doc/html/rfc3986
* @param id the id of the schema containing the definition
* @param name the name of the definition
* @returns the URI pointing to the definition in the schema
*/
function createRefURI(id: string, name: string): string {
const encoded = encodeURIComponent(name);
return `${id}#/definitions/${encoded}`;
}

export function getProgramFromFiles(
files: string[],
jsonCompilerOptions: any = {},
Expand Down
Loading