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

Export additionalProperties as intersection for complex types #383

Closed
wants to merge 4 commits into from
Closed
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
52 changes: 50 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
isPrimitive,
JSONSchema as LinkedJSONSchema,
JSONSchemaWithDefinitions,
NormalizedJSONSchema,
SchemaSchema,
SchemaType
} from './types/JSONSchema'
Expand Down Expand Up @@ -313,16 +314,63 @@ function newInterface(
usedNames: UsedNames,
keyName?: string,
keyNameFromDefinition?: string
): TInterface {
): TInterface | TIntersection {
let complexAdditionalProperties: false | NormalizedJSONSchema

if (schema.additionalProperties !== undefined && typeof schema.additionalProperties !== 'boolean') {
complexAdditionalProperties = schema.additionalProperties
schema.additionalProperties = false
} else {
complexAdditionalProperties = false
}

const name = standaloneName(schema, keyNameFromDefinition, usedNames)!
return {

const parsedInterface: TInterface = {
comment: schema.description,
keyName,
params: parseSchema(schema, options, processed, usedNames, name),
standaloneName: name,
superTypes: parseSuperTypes(schema, options, processed, usedNames),
type: 'INTERFACE'
}

if (complexAdditionalProperties === false) {
return parsedInterface
} else {
const parsedAdditionalProperties: TInterface = {
params: [
{
ast: parse(complexAdditionalProperties, options, '[k: string]', processed, usedNames),
isPatternProperty: false,
isRequired: true,
isUnreachableDefinition: false,
keyName: '[k: string]'
}
],
superTypes: [],
type: 'INTERFACE'
}

if (parsedInterface.params.length > 0) {
delete parsedInterface.standaloneName
return {
comment: schema.description,
keyName,
standaloneName: name,
params: [parsedInterface, parsedAdditionalProperties],
type: 'INTERSECTION'
}
} else {
// there were only additionalProperties
return {
...parsedAdditionalProperties,
comment: schema.description,
keyName,
standaloneName: name
}
}
}
}

function parseSuperTypes(
Expand Down
51 changes: 35 additions & 16 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ Generated by [AVA](https://avajs.dev).
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface AdditionalProperties {␊
export type AdditionalProperties = {␊
foo?: string;␊
} & {␊
[k: string]: number;␊
}␊
};
`

## additionalProperties.2.js
Expand Down Expand Up @@ -1271,6 +1272,15 @@ Generated by [AVA](https://avajs.dev).
[k: string]: unknown;␊
};␊
export type Header1 = ExampleXORExamples & SchemaXORContent;␊
export type Callback = {␊
/**␊
* This interface was referenced by \`Callback\`'s JSON-Schema definition␊
* via the \`patternProperty\` "^x-".␊
*/␊
[k: string]: unknown;␊
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we improve this output?

} & {␊
[k: string]: PathItem;␊
};␊
export type SecurityScheme =␊
| APIKeySecurityScheme␊
| HTTPSecurityScheme␊
Expand Down Expand Up @@ -1579,9 +1589,6 @@ Generated by [AVA](https://avajs.dev).
*/␊
[k: string]: unknown;␊
}␊
export interface Callback {␊
[k: string]: PathItem;␊
}␊
export interface Components {␊
schemas?: {␊
/**␊
Expand Down Expand Up @@ -2132,10 +2139,11 @@ Generated by [AVA](https://avajs.dev).
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface StrictIndexSignatures {␊
export type StrictIndexSignatures = {␊
maybe?: string;␊
} & {␊
[k: string]: string | undefined;␊
}␊
};
`

## options.style.js
Expand Down Expand Up @@ -2326,15 +2334,16 @@ Generated by [AVA](https://avajs.dev).
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export type Parent = {␊
[k: string]: StringChild;␊
} & {␊
[k: string]: number;␊
};␊
/**␊
* This interface was referenced by \`Parent\`'s JSON-Schema definition␊
* via the \`patternProperty\` "^[a-zA-Z]+".␊
*/␊
export type StringChild = string;␊
export interface Parent {␊
[k: string]: number;␊
}␊
`

## realWorld.awsQuicksight.js
Expand Down Expand Up @@ -444063,6 +444072,15 @@ Generated by [AVA](https://avajs.dev).
[k: string]: unknown;␊
};␊
export type Header1 = ExampleXORExamples & SchemaXORContent;␊
export type Callback = {␊
/**␊
* This interface was referenced by \`Callback\`'s JSON-Schema definition␊
* via the \`patternProperty\` "^x-".␊
*/␊
[k: string]: unknown;␊
} & {␊
[k: string]: PathItem;␊
};␊
export type SecurityScheme =␊
| APIKeySecurityScheme␊
| HTTPSecurityScheme␊
Expand Down Expand Up @@ -444371,9 +444389,6 @@ Generated by [AVA](https://avajs.dev).
*/␊
[k: string]: unknown;␊
}␊
export interface Callback {␊
[k: string]: PathItem;␊
}␊
export interface Components {␊
schemas?: {␊
/**␊
Expand Down Expand Up @@ -447709,6 +447724,7 @@ Generated by [AVA](https://avajs.dev).
prerestart?: ScriptsRestart;␊
restart?: ScriptsRestart;␊
postrestart?: ScriptsRestart;␊
} & {␊
[k: string]: string | undefined;␊
};␊
/**␊
Expand Down Expand Up @@ -447749,6 +447765,7 @@ Generated by [AVA](https://avajs.dev).
};␊
engines?: {␊
node?: string;␊
} & {␊
[k: string]: string;␊
};␊
engineStrict?: boolean;␊
Expand Down Expand Up @@ -447789,11 +447806,12 @@ Generated by [AVA](https://avajs.dev).
*/␊
esnext?:␊
| string␊
| {␊
| ({␊
main?: string;␊
browser?: string;␊
} & {␊
[k: string]: string;␊
};␊
});␊
/**␊
* Allows packages within a directory to depend on one another using direct linking of local files. Additionally, dependencies within a workspace are hoisted to the workspace root when possible to reduce duplication. Note: It's also a good idea to set "private" to true when using this feature.␊
*/␊
Expand Down Expand Up @@ -448598,6 +448616,7 @@ Generated by [AVA](https://avajs.dev).
company?: {␊
name: string;␊
};␊
} & {␊
[k: string]: KString;␊
};␊
[k: string]: unknown;␊
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.