Skip to content

Re-use generated interfaces for $ref in allOf #154

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

Closed
vsund opened this issue Mar 25, 2018 · 4 comments
Closed

Re-use generated interfaces for $ref in allOf #154

vsund opened this issue Mar 25, 2018 · 4 comments

Comments

@vsund
Copy link

vsund commented Mar 25, 2018

Hey,

I tried to externalize some complex properties from my main schema Person.json to use the (sub) interfaces separately in the code.
For example my Person.json schema has an account property and I put the schema from this into components/Account.json.

It would be cool to re-use the declared interfaces for these types. E.g.

export type Person = {
	name?: string;
	account?: Account[];
	[k: string]: any;
};

For all the used schemas etc see below. Please note that I simplified the schema, if something doesn't match please let me know.


Person.json
{
"$schema": "http://json-schema.org/schema#",
	"type": "object",
	"properties": {
		"name": { "type": "string" },
		"account": {
			"type": "array",
			"items": {
				"allOf": [{ "$ref": "components/Account.json" }]
			}
		}
	}
}
components/Account.json
{
	"$schema": "http://json-schema.org/schema#",
	"type": "object",
	"properties": {
		"@type": { "type": "string" },
		"role": { "type": "string" },
		"service": { "type": "string" },
		"identifier": { "type": "string" },
		"proofType": { "type": "string" },
		"proofUrl": { "type": "string" },
		"proofMessage": { "type": "string" },
		"proofSignature": { "type": "string" }
	}
}
Person.json.d.ts
export type Person = {
	name?: string;
	account?: {
		'@type'?: string;
		role?: string;
		service?: string;
		identifier?: string;
		proofType?: string;
		proofUrl?: string;
		proofMessage?: string;
		proofSignature?: string;
		[k: string]: any;
	}[];
	[k: string]: any;
};
components/Account.json.d.ts
export interface Account {
	'@type'?: string;
	role?: string;
	service?: string;
	identifier?: string;
	proofType?: string;
	proofUrl?: string;
	proofMessage?: string;
	proofSignature?: string;
	[k: string]: any;
}
@bcherny
Copy link
Owner

bcherny commented Mar 25, 2018

Hi @vsund! We use the presence of the id property as a heuristic for whether or not to pull out a subschema into its own type alias. Try adding id to Account.json's schema.

You might run into an issue where we generate the linked schema twice - let me know what the result is!

@vsund
Copy link
Author

vsund commented Mar 25, 2018

Thanks for the help! Indeed generated twice:

Person.json

export type Person = {
	name?: string;
	account?: AccountJson[];
	[k: string]: any;
};

export interface AccountJson {
	'@type'?: string;
	role?: string;
	service?: string;
	identifier?: string;
	proofType?: string;
	proofUrl?: string;
	proofMessage?: string;
	proofSignature?: string;
	[k: string]: any;
}

The same AccountJson interface is is also in components/Account.json. Anything I could/should do about this?

@bcherny
Copy link
Owner

bcherny commented Mar 25, 2018

This is a known issue #16. You could avoid compiling Account.json for now, or you could send a PR for the issue :)

In either case this shouldn't affect anything -- TS will merge the two AccountJson declarations for you automatically, so while the output is ugly, it works well.

@vsund
Copy link
Author

vsund commented Mar 25, 2018

Yeah, it's basically only the ugly output. But since I don't commit these files I think it's ok :)

Thanks for all the help! ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants