Skip to content

Commit a2c0f11

Browse files
committed
Merge inherited type properties. Fixes #41
1 parent 06fd1c3 commit a2c0f11

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/metadata/resolveType.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ function getReferenceType(type: ts.EntityName, genericTypeMap?: Map<String, ts.T
238238
}
239239

240240
const extendedProperties = getInheritedProperties(modelTypeDeclaration, genericTypes);
241-
referenceType.properties = referenceType.properties.concat(extendedProperties);
241+
mergeReferenceTypeProperties(referenceType.properties, extendedProperties);
242242

243243
localReferenceTypeCache[typeNameWithGenerics] = referenceType;
244244

@@ -249,6 +249,17 @@ function getReferenceType(type: ts.EntityName, genericTypeMap?: Map<String, ts.T
249249
}
250250
}
251251

252+
function mergeReferenceTypeProperties(properties: Property[], extendedProperties: Property[]) {
253+
extendedProperties.forEach(prop => {
254+
const existingProp = properties.find(p => p.name === prop.name);
255+
if (existingProp) {
256+
existingProp.description = existingProp.description || prop.description;
257+
} else {
258+
properties.push(prop);
259+
}
260+
});
261+
}
262+
252263
function resolveFqTypeName(type: ts.EntityName): string {
253264
if (type.kind === ts.SyntaxKind.Identifier) {
254265
return (type as ts.Identifier).text;

test/data/apis.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,23 @@ export class ParameterizedEndpoint {
328328
return new PrimitiveClassModel();
329329
}
330330
}
331+
332+
export abstract class Entity {
333+
/**
334+
* A numeric identifier
335+
*/
336+
id?: number;
337+
}
338+
339+
export class NamedEntity implements Entity {
340+
id: number;
341+
name: string;
342+
}
343+
344+
@Path('abstract')
345+
export class AbstractEntityEndpoint {
346+
@GET
347+
get(): NamedEntity {
348+
return new NamedEntity();
349+
}
350+
}

test/unit/definitions.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,16 @@ describe('Definition generation', () => {
284284
expect(expression.evaluate(spec)).to.eq('path');
285285
});
286286
});
287+
288+
describe('AbstractEntityEndpoint', () => {
289+
it('should not duplicate inherited properties in the required list', () => {
290+
const expression = jsonata('definitions.NamedEntity.required');
291+
expect(expression.evaluate(spec)).to.deep.equal(['id', 'name']);
292+
});
293+
294+
it('should use property description from base class if not defined in child', () => {
295+
const expression = jsonata('definitions.NamedEntity.properties.id.description');
296+
expect(expression.evaluate(spec)).to.eq('A numeric identifier');
297+
});
298+
});
287299
});

0 commit comments

Comments
 (0)