Skip to content

Export additionalProperties as intersection for complex types #383

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Loading