diff --git a/__fixtures__/issues/103/raw/duplicate.json b/__fixtures__/issues/103/raw/duplicate.json new file mode 100644 index 00000000..22b92543 --- /dev/null +++ b/__fixtures__/issues/103/raw/duplicate.json @@ -0,0 +1,3 @@ +{ + "contract_name": "something-in-raw-folder" +} \ No newline at end of file diff --git a/__fixtures__/issues/103/schema.json b/__fixtures__/issues/103/schema.json new file mode 100644 index 00000000..25c934ae --- /dev/null +++ b/__fixtures__/issues/103/schema.json @@ -0,0 +1,58 @@ +{ + "contract_name": "cw1-whitelist", + "contract_version": "0.3.0", + "idl_version": "1.0.0", + "instantiate": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "required": [ + "admins", + "mutable" + ], + "properties": { + "admins": { + "type": "array", + "items": { + "type": "string" + } + }, + "mutable": { + "type": "boolean" + } + } + }, + "execute": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "anyOf": [ + { + "$ref": "#/definitions/ExecMsg" + } + ], + "definitions": { + "ExecMsg": { + "type": "string", + "enum": [] + } + } + }, + "query": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "anyOf": [ + { + "$ref": "#/definitions/QueryMsg" + } + ], + "definitions": { + "QueryMsg": { + "type": "string", + "enum": [] + } + } + }, + "migrate": null, + "sudo": null, + "responses": {} +} \ No newline at end of file diff --git a/__output__/issues/103/clean.json b/__output__/issues/103/clean.json new file mode 100644 index 00000000..a799739a --- /dev/null +++ b/__output__/issues/103/clean.json @@ -0,0 +1,109 @@ +{ + "schemas": [ + { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "required": [ + "admins", + "mutable" + ], + "properties": { + "admins": { + "type": "array", + "items": { + "type": "string" + } + }, + "mutable": { + "type": "boolean" + } + } + }, + { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "anyOf": [ + { + "$ref": "#/definitions/ExecMsg" + } + ], + "definitions": { + "ExecMsg": { + "type": "string" + } + } + }, + { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "anyOf": [ + { + "$ref": "#/definitions/QueryMsg" + } + ], + "definitions": { + "QueryMsg": { + "type": "string" + } + } + } + ], + "responses": {}, + "idlObject": { + "contract_name": "cw1-whitelist", + "contract_version": "0.3.0", + "idl_version": "1.0.0", + "instantiate": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "required": [ + "admins", + "mutable" + ], + "properties": { + "admins": { + "type": "array", + "items": { + "type": "string" + } + }, + "mutable": { + "type": "boolean" + } + } + }, + "execute": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "anyOf": [ + { + "$ref": "#/definitions/ExecMsg" + } + ], + "definitions": { + "ExecMsg": { + "type": "string" + } + } + }, + "query": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "anyOf": [ + { + "$ref": "#/definitions/QueryMsg" + } + ], + "definitions": { + "QueryMsg": { + "type": "string" + } + } + }, + "migrate": null, + "sudo": null, + "responses": {} + } +} \ No newline at end of file diff --git a/__output__/issues/103/orig.json b/__output__/issues/103/orig.json new file mode 100644 index 00000000..b4c7a8f0 --- /dev/null +++ b/__output__/issues/103/orig.json @@ -0,0 +1,113 @@ +{ + "schemas": [ + { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "required": [ + "admins", + "mutable" + ], + "properties": { + "admins": { + "type": "array", + "items": { + "type": "string" + } + }, + "mutable": { + "type": "boolean" + } + } + }, + { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "anyOf": [ + { + "$ref": "#/definitions/ExecMsg" + } + ], + "definitions": { + "ExecMsg": { + "type": "string", + "enum": [] + } + } + }, + { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "anyOf": [ + { + "$ref": "#/definitions/QueryMsg" + } + ], + "definitions": { + "QueryMsg": { + "type": "string", + "enum": [] + } + } + } + ], + "responses": {}, + "idlObject": { + "contract_name": "cw1-whitelist", + "contract_version": "0.3.0", + "idl_version": "1.0.0", + "instantiate": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "required": [ + "admins", + "mutable" + ], + "properties": { + "admins": { + "type": "array", + "items": { + "type": "string" + } + }, + "mutable": { + "type": "boolean" + } + } + }, + "execute": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "anyOf": [ + { + "$ref": "#/definitions/ExecMsg" + } + ], + "definitions": { + "ExecMsg": { + "type": "string", + "enum": [] + } + } + }, + "query": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "anyOf": [ + { + "$ref": "#/definitions/QueryMsg" + } + ], + "definitions": { + "QueryMsg": { + "type": "string", + "enum": [] + } + } + }, + "migrate": null, + "sudo": null, + "responses": {} + } +} \ No newline at end of file diff --git a/packages/ts-codegen/__tests__/cleanse.test.ts b/packages/ts-codegen/__tests__/cleanse.test.ts index c60a838d..6b79b3ab 100644 --- a/packages/ts-codegen/__tests__/cleanse.test.ts +++ b/packages/ts-codegen/__tests__/cleanse.test.ts @@ -29,6 +29,17 @@ it('daodao/cw-code-id-registry', async () => { mkdirp(out); writeFileSync(out + '/orig.json', JSON.stringify(orig, null, 2)); writeFileSync(out + '/clean.json', JSON.stringify(clean, null, 2)); +}) + +it('issues/103', async () => { + const out = OUTPUT_DIR + '/issues/103'; + const schemaDir = FIXTURE_DIR + '/issues/103/'; + + const clean = await readSchemas({ schemaDir, clean: true }); + const orig = await readSchemas({ schemaDir, clean: false }); + mkdirp(out); + writeFileSync(out + '/orig.json', JSON.stringify(orig, null, 2)); + writeFileSync(out + '/clean.json', JSON.stringify(clean, null, 2)); }) diff --git a/packages/ts-codegen/src/utils/schemas.ts b/packages/ts-codegen/src/utils/schemas.ts index 8d4ffcaa..a1206a65 100644 --- a/packages/ts-codegen/src/utils/schemas.ts +++ b/packages/ts-codegen/src/utils/schemas.ts @@ -13,7 +13,9 @@ export const readSchemas = async ({ schemaDir, clean = true }: ReadSchemaOpts): Promise => { const fn = clean ? cleanse : (str) => str; - const files = glob(schemaDir + '/**/*.json'); + const files = glob(schemaDir + '/**/*.json') + .filter(file => !file.match(/\/raw\//)); + const schemas = files .map(file => JSON.parse(readFileSync(file, 'utf-8'))); diff --git a/packages/wasm-ast-types/src/client/test/__snapshots__/ts-client.issue-103.test.ts.snap b/packages/wasm-ast-types/src/client/test/__snapshots__/ts-client.issue-103.test.ts.snap new file mode 100644 index 00000000..ba311655 --- /dev/null +++ b/packages/wasm-ast-types/src/client/test/__snapshots__/ts-client.issue-103.test.ts.snap @@ -0,0 +1,79 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`execute execute classes array types 1`] = ` +"export class Client implements Instance { + client: SigningCosmWasmClient; + sender: string; + contractAddress: string; + + constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) { + this.client = client; + this.sender = sender; + this.contractAddress = contractAddress; + } + +}" +`; + +exports[`execute execute interfaces no extends 1`] = ` +"export interface SG721Instance { + contractAddress: string; + sender: string; +}" +`; + +exports[`execute execute_msg_for__empty 1`] = `"export type ExecuteMsg = ExecuteMsg;"`; + +exports[`execute query classes 1`] = ` +"export class QueryClient implements ReadOnlyInstance { + client: CosmWasmClient; + contractAddress: string; + + constructor(client: CosmWasmClient, contractAddress: string) { + this.client = client; + this.contractAddress = contractAddress; + } + +}" +`; + +exports[`execute query classes response 1`] = `"export type QueryMsg = QueryMsg;"`; + +exports[`query execute classes array types 1`] = ` +"export class Client implements Instance { + client: SigningCosmWasmClient; + sender: string; + contractAddress: string; + + constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) { + this.client = client; + this.sender = sender; + this.contractAddress = contractAddress; + } + +}" +`; + +exports[`query execute interfaces no extends 1`] = ` +"export interface SG721Instance { + contractAddress: string; + sender: string; +}" +`; + +exports[`query execute_msg_for__empty 1`] = `"export type QueryMsg = QueryMsg;"`; + +exports[`query query classes 1`] = ` +"export class QueryClient implements ReadOnlyInstance { + client: CosmWasmClient; + contractAddress: string; + + constructor(client: CosmWasmClient, contractAddress: string) { + this.client = client; + this.contractAddress = contractAddress; + } + +}" +`; + +exports[`query query classes response 1`] = `"export type QueryMsg = QueryMsg;"`; diff --git a/packages/wasm-ast-types/src/client/test/ts-client.issue-103.test.ts b/packages/wasm-ast-types/src/client/test/ts-client.issue-103.test.ts new file mode 100644 index 00000000..4f394115 --- /dev/null +++ b/packages/wasm-ast-types/src/client/test/ts-client.issue-103.test.ts @@ -0,0 +1,106 @@ +import contract from '../../../../../__fixtures__/issues/103/schema.json'; + +import { + createQueryClass, + createExecuteClass, + createExecuteInterface, + createTypeInterface +} from '../client' +import { expectCode, printCode, makeContext } from '../../../test-utils'; + +const queryMessage = contract.query +const executeMessage = contract.execute +const queryCtx = makeContext(queryMessage); +const executeCtx = makeContext(executeMessage); + +describe('query', () => { + it('execute_msg_for__empty', () => { + expectCode(createTypeInterface( + queryCtx, + queryMessage + )) + }) + + + it('query classes', () => { + expectCode(createQueryClass( + queryCtx, + 'QueryClient', + 'ReadOnlyInstance', + queryMessage + )) + }); + + it('query classes response', () => { + expectCode(createTypeInterface( + queryCtx, + contract.query + )) + }); + + it('execute classes array types', () => { + expectCode(createExecuteClass( + queryCtx, + 'Client', + 'Instance', + null, + queryMessage + )) + }); + + it('execute interfaces no extends', () => { + expectCode(createExecuteInterface( + queryCtx, + 'SG721Instance', + null, + queryMessage + )) + }); + +}); + +describe('execute', () => { + it('execute_msg_for__empty', () => { + expectCode(createTypeInterface( + executeCtx, + executeMessage + )) + }) + + + it('query classes', () => { + expectCode(createQueryClass( + executeCtx, + 'QueryClient', + 'ReadOnlyInstance', + executeMessage + )) + }); + + it('query classes response', () => { + expectCode(createTypeInterface( + executeCtx, + contract.query + )) + }); + + it('execute classes array types', () => { + expectCode(createExecuteClass( + executeCtx, + 'Client', + 'Instance', + null, + executeMessage + )) + }); + + it('execute interfaces no extends', () => { + expectCode(createExecuteInterface( + executeCtx, + 'SG721Instance', + null, + executeMessage + )) + }); + +});