Skip to content

Commit d4b2380

Browse files
authored
fix-uber-parent-for-multi-parents-inheritance (#2527)
1 parent 778140a commit d4b2380

2 files changed

Lines changed: 56 additions & 30 deletions

File tree

packages/autorest.typescript/src/transforms/mapperTransforms.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ const primitiveSchemaTypes = [
5858
* if any function processed the input, remaining ones will skip
5959
* @param fns functions in the pipeline
6060
*/
61-
const pipe = (
62-
...fns: Array<(pipelineValue: PipelineValue) => PipelineValue>
63-
) => (x: PipelineValue) => fns.reduce((v, f) => (!v.isHandled ? f(v) : v), x);
61+
const pipe =
62+
(...fns: Array<(pipelineValue: PipelineValue) => PipelineValue>) =>
63+
(x: PipelineValue) =>
64+
fns.reduce((v, f) => (!v.isHandled ? f(v) : v), x);
6465

6566
export type ModelProperties = { [propertyName: string]: Mapper | string[] };
6667

@@ -91,12 +92,12 @@ export async function transformMappers(
9192
return [];
9293
}
9394

94-
const uberParentsNames = uberParents.map(up => up.name);
95+
const uberParentsNames = uberParents.map((up) => up.name);
9596
const hasXmlMetadata = mediaTypes?.has(KnownMediaType.Xml);
9697
return [
9798
...codeModel.schemas.objects,
9899
...extractHeaders(codeModel.operationGroups, clientName)
99-
].map(objectSchema =>
100+
].map((objectSchema) =>
100101
transformMapper({
101102
schema: objectSchema,
102103
options: {
@@ -275,13 +276,17 @@ function getXmlMetadata(
275276

276277
const defaultName =
277278
serializedName || getLanguageMetadata(schema.language).serializedName;
278-
const { name, attribute: xmlIsAttribute, wrapped: xmlIsWrapped } =
279-
schema.serialization?.xml || {};
279+
const {
280+
name,
281+
attribute: xmlIsAttribute,
282+
wrapped: xmlIsWrapped
283+
} = schema.serialization?.xml || {};
280284

281285
const xmlName = name || defaultName;
282286

283-
const headerCollectionPrefix = getLanguageMetadata(schema.language)
284-
.headerCollectionPrefix;
287+
const headerCollectionPrefix = getLanguageMetadata(
288+
schema.language
289+
).headerCollectionPrefix;
285290

286291
return {
287292
...(headerCollectionPrefix && { headerCollectionPrefix }),
@@ -330,8 +335,8 @@ function transformObjectMapper(pipelineValue: PipelineValue) {
330335
true /** immediateOnly */
331336
);
332337
const parentsRefs = immediateParents
333-
.map(p => getMapperClassName(p))
334-
.filter(p => p !== className);
338+
.map((p) => getMapperClassName(p))
339+
.filter((p) => p !== className);
335340

336341
const additionalProperties = buildAdditionalProperties(objectSchema);
337342

@@ -350,12 +355,20 @@ function transformObjectMapper(pipelineValue: PipelineValue) {
350355
// If any of the parents is present in uberParents we know it
351356
// is its uber parent
352357
let uberParent = getMapperClassName(
353-
parents.find(p => uberParents.includes(getMapperClassName(p))) || schema
358+
parents.find(
359+
(p) =>
360+
(p as ObjectSchema).discriminator &&
361+
uberParents.includes(getMapperClassName(p))
362+
) ||
363+
parents.find((p) => uberParents.includes(getMapperClassName(p))) ||
364+
schema
354365
);
355366

356367
if (objectSchema.parents?.immediate[0]) {
357368
uberParent = getMapperClassName(
358-
objectSchema.parents?.immediate[0] as ObjectSchema
369+
objectSchema.parents?.immediate.find(
370+
(im) => (im as ObjectSchema).discriminator
371+
) || (objectSchema.parents?.immediate[0] as ObjectSchema)
359372
);
360373
}
361374
const mapper = buildMapper(
@@ -527,7 +540,7 @@ function transformChoiceMapper(pipelineValue: PipelineValue) {
527540
} else {
528541
type = {
529542
name: MapperType.Enum,
530-
allowedValues: choiceSchema.choices.map(choice => choice.value)
543+
allowedValues: choiceSchema.choices.map((choice) => choice.value)
531544
};
532545
}
533546
}
@@ -703,7 +716,7 @@ function processProperties(
703716
options: EntityOptions = {}
704717
) {
705718
let modelProperties: ModelProperties = {};
706-
properties.forEach(prop => {
719+
properties.forEach((prop) => {
707720
const serializedName = getPropertySerializedName(prop);
708721
const propName = getLanguageMetadata(prop.language).name;
709722
const name = normalizeName(
@@ -734,9 +747,9 @@ function getPropertySerializedName({
734747
}
735748

736749
return flattenedNames
737-
.map(name => {
750+
.map((name) => {
738751
// Escaping names
739-
["."].forEach(character => {
752+
["."].forEach((character) => {
740753
name = name.replace(character, `\\${character}`);
741754
});
742755
return name;

packages/autorest.typescript/src/transforms/objectTransforms.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ export function transformObject(
6363
description: metadata.description || undefined,
6464
schema,
6565
properties: schema.properties
66-
? schema.properties.map((prop) =>
67-
transformProperty(prop)
68-
)
66+
? schema.properties.map((prop) => transformProperty(prop))
6967
: []
7068
};
7169

@@ -246,15 +244,28 @@ function transformPolymorphicObject(
246244
let uberParent: ObjectSchema | undefined = objectDetails.schema;
247245
const allParents = getSchemaParents(schema);
248246
if (allParents && allParents.length) {
249-
const uberParentSchema = allParents.find((p) => {
250-
// TODO: Reconsider names to reduce issues with normalization, can we switch to serialized?
251-
const name = normalizeName(
252-
getLanguageMetadata(p.language).name,
253-
NameType.Interface,
254-
true /** shouldGuard */
255-
);
256-
return uberParents.some((up) => up.name === name);
257-
});
247+
const uberParentSchema =
248+
allParents.find((p) => {
249+
// TODO: Reconsider names to reduce issues with normalization, can we switch to serialized?
250+
const name = normalizeName(
251+
getLanguageMetadata(p.language).name,
252+
NameType.Interface,
253+
true /** shouldGuard */
254+
);
255+
return (
256+
(p as ObjectSchema).discriminator &&
257+
uberParents.some((up) => up.name === name)
258+
);
259+
}) ||
260+
allParents.find((p) => {
261+
// TODO: Reconsider names to reduce issues with normalization, can we switch to serialized?
262+
const name = normalizeName(
263+
getLanguageMetadata(p.language).name,
264+
NameType.Interface,
265+
true /** shouldGuard */
266+
);
267+
return uberParents.some((up) => up.name === name);
268+
});
258269

259270
if (!uberParentSchema) {
260271
const upn = uberParents.map((u) => u.name);
@@ -271,7 +282,9 @@ function transformPolymorphicObject(
271282
}
272283

273284
if (schema !== uberParent && objectDetails.schema.parents?.immediate[0]) {
274-
uberParent = objectDetails.schema.parents?.immediate[0] as ObjectSchema;
285+
uberParent = (objectDetails.schema.parents?.immediate.find(
286+
(im) => (im as ObjectSchema).discriminator
287+
) || objectDetails.schema.parents?.immediate[0]) as ObjectSchema;
275288
}
276289
const uberParentName = getLanguageMetadata(uberParent.language).name;
277290
const unionName = `${normalizeName(uberParentName, NameType.Interface)}Union`;

0 commit comments

Comments
 (0)