Skip to content

Commit

Permalink
enable allOf with non-ref schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
spokli committed Oct 30, 2023
1 parent c6ed409 commit adcb2c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/classes/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export class Ref {

getImportAndTypeByRef = (ref: string, path: string): IRefResult => {
const reference = this.refs[ref];
if(!reference) {
throw new Error(`Reference ${ref} not found.`);
}
let relativePath = relative(path, reference.folderPath).split(sep).join('/');
if (!relativePath) {
relativePath = './';
Expand Down
26 changes: 23 additions & 3 deletions src/function/get-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ const getProperty = (className: string, originalName: string, propertyName: stri
if (isDebug) {
console.log(`Enter get property ${originalName}`, JSON.stringify(schema, null, 2))
}
if (schema.allOf) {
schema = schema.allOf.find(v => !!v['$ref']) as any
}
let isArray = false;


Expand Down Expand Up @@ -131,6 +128,10 @@ const getProperty = (className: string, originalName: string, propertyName: stri
console.log('Reference found ', JSON.stringify(schema, null, 2))
}

if (schema.allOf) {
reference = handleAllOfProperty(propertyName, originalName, path, schema)
}

if (!!enumeration) {
let newOriginal = `${propertyName}${originalName.substring(0, 1).toUpperCase()}${originalName.substring(1)}`;
const nestedPath = getNestedPath(path, 'enumeration');
Expand Down Expand Up @@ -225,3 +226,22 @@ const isPrimitive = (type: string | undefined) => {
return false

}


const handleAllOfProperty = (propertyName: string, originalName: string, path: string, schema: ISchema) => {
const ref = configuration.getReference();

let referenceName = `${propertyName}${originalName.substring(0, 1).toUpperCase()}${originalName.substring(1)}`;

const allOfCombination = getInterfaceOrEnumFromSchema(`I${formatText(referenceName, 'ANY', 'PascalCase')}`, referenceName, schema, path) as EnumProperty;
const rendered = allOfCombination.render();
fs.appendFileSync(nodePath.join(path, `${rendered.fileName}.ts`), rendered.render)

const refKey = `#/nested/${referenceName}`
ref.addReference(refKey, {
fileName: rendered.fileName,
className: rendered.classEnumName,
folderPath: path
})
return refKey;
}

0 comments on commit adcb2c9

Please sign in to comment.