diff --git a/packages/@ngtools/json-schema/src/serializers/json.spec.ts b/packages/@ngtools/json-schema/src/serializers/json.spec.ts index ef6f36563dfb..bf8465161a21 100644 --- a/packages/@ngtools/json-schema/src/serializers/json.spec.ts +++ b/packages/@ngtools/json-schema/src/serializers/json.spec.ts @@ -7,26 +7,28 @@ import {RootSchemaTreeNode} from '../schema-tree'; describe('JsonSerializer', () => { - const schemaJsonFilePath = path.join(__dirname, '../../tests/schema1.json'); - const schemaJson = JSON.parse(fs.readFileSync(schemaJsonFilePath, 'utf-8')); - const valueJsonFilePath = path.join(__dirname, '../../tests/value1.json'); - const valueJson = JSON.parse(fs.readFileSync(valueJsonFilePath, 'utf-8')); - - const schemaClass = new (SchemaClassFactory(schemaJson))(valueJson); - const schema: RootSchemaTreeNode = schemaClass.$$schema(); - - it('works', () => { - let str = ''; - function writer(s: string) { - str += s; - } - - const serializer = new JsonSerializer(writer); - - serializer.start(); - schema.serialize(serializer); - serializer.end(); - - expect(JSON.stringify(JSON.parse(str))).toEqual(JSON.stringify(valueJson)); - }); + 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 valueJsonFilePath = path.join(__dirname, `../../tests/serializer/value${nb}.json`); + const valueJson = JSON.parse(fs.readFileSync(valueJsonFilePath, 'utf-8')); + + const schemaClass = new (SchemaClassFactory(schemaJson))(valueJson); + const schema: RootSchemaTreeNode = schemaClass.$$schema(); + + let str = ''; + function writer(s: string) { + str += s; + } + + const serializer = new JsonSerializer(writer); + + serializer.start(); + schema.serialize(serializer); + serializer.end(); + + expect(JSON.stringify(JSON.parse(str))).toEqual(JSON.stringify(valueJson)); + }); + } }); diff --git a/packages/@ngtools/json-schema/src/serializers/json.ts b/packages/@ngtools/json-schema/src/serializers/json.ts index 1ab57575ffd7..156778088db3 100644 --- a/packages/@ngtools/json-schema/src/serializers/json.ts +++ b/packages/@ngtools/json-schema/src/serializers/json.ts @@ -77,7 +77,7 @@ export class JsonSerializer implements Serializer { this._willOutputValue(); this._writer(JSON.stringify(key)); this._writer(': '); - this._writer(JSON.stringify(node.value)); + this._writer(JSON.stringify(node.value[key])); } } diff --git a/packages/@ngtools/json-schema/tests/serializer/schema1.json b/packages/@ngtools/json-schema/tests/serializer/schema1.json new file mode 100644 index 000000000000..983feceb8bcd --- /dev/null +++ b/packages/@ngtools/json-schema/tests/serializer/schema1.json @@ -0,0 +1,84 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "JsonSchema", + "type": "object", + "properties": { + "requiredKey": { + "type": "number" + }, + "stringKeyDefault": { + "type": "string", + "default": "defaultValue" + }, + "stringKey": { + "type": "string" + }, + "booleanKey": { + "type": "boolean" + }, + "numberKey": { + "type": "number" + }, + "oneOfKey1": { + "oneOf": [ + { "type": "string" }, + { "type": "number" } + ] + }, + "oneOfKey2": { + "oneOf": [ + { "type": "string" }, + { "type": "array", "items": { "type": "string" } } + ] + }, + "objectKey1": { + "type": "object", + "properties": { + "stringKey": { + "type": "string" + }, + "objectKey": { + "type": "object", + "properties": { + "stringKey": { + "type": "string" + } + } + } + } + }, + "objectKey2": { + "type": "object", + "properties": { + "stringKey": { + "type": "string", + "default": "default objectKey2.stringKey" + } + }, + "additionalProperties": true + }, + "arrayKey1": { + "type": "array", + "items": { + "type": "object", + "properties": { + "stringKey": { + "type": "string" + } + } + } + }, + "arrayKey2": { + "type": "array", + "items": { + "type": "object", + "properties": { + "stringKey": { + "type": "string" + } + } + } + } + }, + "required": ["requiredKey"] +} \ No newline at end of file diff --git a/packages/@ngtools/json-schema/tests/serializer/schema2.json b/packages/@ngtools/json-schema/tests/serializer/schema2.json new file mode 100644 index 000000000000..8afa29a93b42 --- /dev/null +++ b/packages/@ngtools/json-schema/tests/serializer/schema2.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "JsonSchema", + "type": "object", + "properties": { + "a": { + "type": "array", + "items": { + "enum": [ "v1", "v2", "v3" ] + } + } + } +} diff --git a/packages/@ngtools/json-schema/tests/serializer/schema3.json b/packages/@ngtools/json-schema/tests/serializer/schema3.json new file mode 100644 index 000000000000..c8626d1aac1d --- /dev/null +++ b/packages/@ngtools/json-schema/tests/serializer/schema3.json @@ -0,0 +1,237 @@ +{ + "$comment": "Please run `npm run build-config-interface` after changing this file. Thanks!", + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "CliConfig", + "title": "Angular CLI Config Schema", + "type": "object", + "properties": { + "project": { + "description": "The global configuration of the project.", + "type": "object", + "properties": { + "version": { + "type": "string" + }, + "name": { + "type": "string" + } + }, + "additionalProperties": false + }, + "apps": { + "description": "Properties of the different applications in this project.", + "type": "array", + "items": { + "type": "object", + "properties": { + "root": { + "type": "string" + }, + "outDir": { + "type": "string", + "default": "dist/" + }, + "assets": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "type": "string" + } + } + ], + "default": [] + }, + "deployUrl": { + "type": "string" + }, + "index": { + "type": "string", + "default": "index.html" + }, + "main": { + "type": "string" + }, + "test": { + "type": "string" + }, + "tsconfig": { + "type": "string", + "default": "tsconfig.json" + }, + "prefix": { + "type": "string" + }, + "mobile": { + "type": "boolean" + }, + "styles": { + "description": "Global styles to be included in the build.", + "type": "array", + "items": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "object", + "properties": { + "input": { + "type": "string" + } + }, + "additionalProperties": true + } + ] + }, + "additionalProperties": false + }, + "scripts": { + "description": "Global scripts to be included in the build.", + "type": "array", + "items": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "object", + "properties": { + "input": { + "type": "string" + } + }, + "additionalProperties": true, + "required": ["input"] + } + ] + }, + "additionalProperties": false + }, + "environments": { + "description": "Name and corresponding file for environment config.", + "type": "object", + "additionalProperties": true + } + }, + "additionalProperties": false + }, + "additionalProperties": false + }, + "addons": { + "description": "Configuration reserved for installed third party addons.", + "type": "array", + "items": { + "type": "object", + "properties": {}, + "additionalProperties": true + } + }, + "packages": { + "description": "Configuration reserved for installed third party packages.", + "type": "array", + "items": { + "type": "object", + "properties": {}, + "additionalProperties": true + } + }, + "e2e": { + "type": "object", + "properties": { + "protractor": { + "type": "object", + "properties": { + "config": { + "type": "string" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + "test": { + "type": "object", + "properties": { + "karma": { + "type": "object", + "properties": { + "config": { + "type": "string" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + "defaults": { + "type": "object", + "properties": { + "styleExt": { + "type": "string" + }, + "prefixInterfaces": { + "type": "boolean" + }, + "poll": { + "type": "number" + }, + "viewEncapsulation": { + "type": "string" + }, + "changeDetection": { + "type": "string" + }, + "inline": { + "type": "object", + "properties": { + "style": { + "type": "boolean", + "default": false + }, + "template": { + "type": "boolean", + "default": false + } + } + }, + "spec": { + "type": "object", + "properties": { + "class": { + "type": "boolean", + "default": false + }, + "component": { + "type": "boolean", + "default": true + }, + "directive": { + "type": "boolean", + "default": true + }, + "module": { + "type": "boolean", + "default": false + }, + "pipe": { + "type": "boolean", + "default": true + }, + "service": { + "type": "boolean", + "default": true + } + } + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false +} diff --git a/packages/@ngtools/json-schema/tests/serializer/value1.json b/packages/@ngtools/json-schema/tests/serializer/value1.json new file mode 100644 index 000000000000..31f049534148 --- /dev/null +++ b/packages/@ngtools/json-schema/tests/serializer/value1.json @@ -0,0 +1,7 @@ +{ + "requiredKey": 1, + "arrayKey2": [ + { "stringKey": "value1" }, + { "stringKey": "value2" } + ] +} \ No newline at end of file diff --git a/packages/@ngtools/json-schema/tests/serializer/value2.json b/packages/@ngtools/json-schema/tests/serializer/value2.json new file mode 100644 index 000000000000..d464dcc68e46 --- /dev/null +++ b/packages/@ngtools/json-schema/tests/serializer/value2.json @@ -0,0 +1,8 @@ +{ + "a": [ + null, + "v1", + null, + "v3" + ] +} diff --git a/packages/@ngtools/json-schema/tests/serializer/value3.json b/packages/@ngtools/json-schema/tests/serializer/value3.json new file mode 100644 index 000000000000..8d79f3fc30f3 --- /dev/null +++ b/packages/@ngtools/json-schema/tests/serializer/value3.json @@ -0,0 +1,57 @@ +{ + "project": { + "version": "<%= version %>", + "name": "<%= htmlComponentName %>" + }, + "apps": [ + { + "root": "<%= sourceDir %>", + "outDir": "dist", + "assets": [ + "assets", + "favicon.ico" + ], + "index": "index.html", + "main": "main.ts", + "test": "test.ts", + "tsconfig": "tsconfig.json", + "prefix": "<%= prefix %>", + "mobile": true, + "styles": [ + "styles.<%= styleExt %>" + ], + "scripts": [], + "environments": { + "source": "environments/environment.ts", + "dev": "environments/environment.ts", + "prod": "environments/environment.prod.ts" + } + } + ], + "e2e": { + "protractor": { + "config": "./protractor.conf.js" + } + }, + "test": { + "karma": { + "config": "./karma.conf.js" + } + }, + "defaults": { + "styleExt": "<%= styleExt %>", + "prefixInterfaces": false, + "inline": { + "style": false, + "template": false + }, + "spec": { + "class": false, + "component": true, + "directive": false, + "module": false, + "pipe": true, + "service": false + } + } +}