-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@ngtools/json-schema): support enums in d.ts (#4426)
And add better tests. Now enums are typed on their values, not just string. Also add support for undefined if a value is truly undefined. NULL is valid JSON value.
- Loading branch information
Showing
13 changed files
with
196 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 28 additions & 21 deletions
49
packages/@ngtools/json-schema/src/serializers/dts.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as ts from 'typescript'; | ||
|
||
import {DTsSerializer} from './dts'; | ||
import {SchemaClassFactory} from '../schema-class-factory'; | ||
import {RootSchemaTreeNode} from '../schema-tree'; | ||
|
||
|
||
describe('DtsSerializer', () => { | ||
const schemaJsonFilePath = path.join(__dirname, '../../tests/schema1.json'); | ||
const schemaJson = JSON.parse(fs.readFileSync(schemaJsonFilePath, 'utf-8')); | ||
const schemaClass = new (SchemaClassFactory(schemaJson))({}); | ||
const schema: RootSchemaTreeNode = schemaClass.$$schema(); | ||
|
||
it('works', () => { | ||
let str = ''; | ||
function writer(s: string) { | ||
str += s; | ||
} | ||
|
||
const serializer = new DTsSerializer(writer, 'HelloWorld'); | ||
|
||
serializer.start(); | ||
schema.serialize(serializer); | ||
serializer.end(); | ||
|
||
// Expect optional properties to be followed by `?` | ||
expect(str).toMatch(/stringKey\?/); | ||
}); | ||
for (const nb of [1, 2, 3]) { | ||
it(`works (${nb})`, () => { | ||
const schemaJsonFilePath = path.join(__dirname, `../../tests/serializer/schema${nb}.json`); | ||
const schemaJson = JSON.parse(fs.readFileSync(schemaJsonFilePath, 'utf-8')); | ||
const valueDTsFilePath = path.join(__dirname, `../../tests/serializer/value${nb}.d.ts`); | ||
const valueDTs = fs.readFileSync(valueDTsFilePath, 'utf-8'); | ||
const valueSourceFile = ts.createSourceFile('test.d.ts', valueDTs, ts.ScriptTarget.Latest); | ||
|
||
const schemaClass = new (SchemaClassFactory(schemaJson))({}); | ||
const schema: RootSchemaTreeNode = schemaClass.$$schema(); | ||
|
||
let str = ''; | ||
function writer(s: string) { | ||
str += s; | ||
} | ||
|
||
const serializer = new DTsSerializer(writer); | ||
|
||
serializer.start(); | ||
schema.serialize(serializer); | ||
serializer.end(); | ||
|
||
const sourceFile = ts.createSourceFile('test.d.ts', str, ts.ScriptTarget.Latest); | ||
expect(sourceFile).toEqual(valueSourceFile); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/@ngtools/json-schema/tests/serializer/value1.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
interface _ { | ||
requiredKey: number; | ||
stringKeyDefault?: string; | ||
stringKey?: string; | ||
booleanKey?: boolean; | ||
numberKey?: number; | ||
oneOfKey1?: (string | number); | ||
oneOfKey2?: (string | string[]); | ||
objectKey1?: { | ||
stringKey?: string; | ||
objectKey?: { | ||
stringKey?: string; | ||
}; | ||
}; | ||
objectKey2?: { | ||
stringKey?: string; | ||
[name: string]: any; | ||
}; | ||
arrayKey1?: { | ||
stringKey?: string; | ||
}[]; | ||
arrayKey2?: { | ||
stringKey?: string; | ||
}[]; | ||
} | ||
export default _; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface _ { | ||
a?: ("v1" | "v2" | "v3")[]; | ||
b?: (0 | 1 | "string" | true | null)[]; | ||
} | ||
export default _; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,11 @@ | |
"v1", | ||
"v2", | ||
"v3" | ||
], | ||
"b": [ | ||
1, | ||
null, | ||
"string", | ||
true | ||
] | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/@ngtools/json-schema/tests/serializer/value3.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
interface _ { | ||
/** | ||
* The global configuration of the project. | ||
*/ | ||
project?: { | ||
version?: string; | ||
name?: string; | ||
}; | ||
/** | ||
* Properties of the different applications in this project. | ||
*/ | ||
apps?: { | ||
root?: string; | ||
outDir?: string; | ||
assets?: (string | string[]); | ||
deployUrl?: string; | ||
index?: string; | ||
main?: string; | ||
test?: string; | ||
tsconfig?: string; | ||
prefix?: string; | ||
/** | ||
* Global styles to be included in the build. | ||
*/ | ||
styles?: (string | { | ||
input?: string; | ||
[name: string]: any; | ||
})[]; | ||
/** | ||
* Global scripts to be included in the build. | ||
*/ | ||
scripts?: (string | { | ||
input: string; | ||
[name: string]: any; | ||
})[]; | ||
/** | ||
* Name and corresponding file for environment config. | ||
*/ | ||
environments?: { | ||
[name: string]: any; | ||
}; | ||
}[]; | ||
/** | ||
* Configuration reserved for installed third party addons. | ||
*/ | ||
addons?: { | ||
[name: string]: any; | ||
}[]; | ||
/** | ||
* Configuration reserved for installed third party packages. | ||
*/ | ||
packages?: { | ||
[name: string]: any; | ||
}[]; | ||
e2e?: { | ||
protractor?: { | ||
config?: string; | ||
}; | ||
}; | ||
test?: { | ||
karma?: { | ||
config?: string; | ||
}; | ||
}; | ||
defaults?: { | ||
styleExt?: string; | ||
prefixInterfaces?: boolean; | ||
poll?: number; | ||
viewEncapsulation?: string; | ||
changeDetection?: string; | ||
inline?: { | ||
style?: boolean; | ||
template?: boolean; | ||
}; | ||
spec?: { | ||
class?: boolean; | ||
component?: boolean; | ||
directive?: boolean; | ||
module?: boolean; | ||
pipe?: boolean; | ||
service?: boolean; | ||
}; | ||
}; | ||
} | ||
export default _; |