Skip to content
Merged
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
47 changes: 30 additions & 17 deletions packages/autorest.typescript/src/transforms/mapperTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ const primitiveSchemaTypes = [
* if any function processed the input, remaining ones will skip
* @param fns functions in the pipeline
*/
const pipe = (
...fns: Array<(pipelineValue: PipelineValue) => PipelineValue>
) => (x: PipelineValue) => fns.reduce((v, f) => (!v.isHandled ? f(v) : v), x);
const pipe =
(...fns: Array<(pipelineValue: PipelineValue) => PipelineValue>) =>
(x: PipelineValue) =>
fns.reduce((v, f) => (!v.isHandled ? f(v) : v), x);

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

Expand Down Expand Up @@ -91,12 +92,12 @@ export async function transformMappers(
return [];
}

const uberParentsNames = uberParents.map(up => up.name);
const uberParentsNames = uberParents.map((up) => up.name);
const hasXmlMetadata = mediaTypes?.has(KnownMediaType.Xml);
return [
...codeModel.schemas.objects,
...extractHeaders(codeModel.operationGroups, clientName)
].map(objectSchema =>
].map((objectSchema) =>
transformMapper({
schema: objectSchema,
options: {
Expand Down Expand Up @@ -275,13 +276,17 @@ function getXmlMetadata(

const defaultName =
serializedName || getLanguageMetadata(schema.language).serializedName;
const { name, attribute: xmlIsAttribute, wrapped: xmlIsWrapped } =
schema.serialization?.xml || {};
const {
name,
attribute: xmlIsAttribute,
wrapped: xmlIsWrapped
} = schema.serialization?.xml || {};

const xmlName = name || defaultName;

const headerCollectionPrefix = getLanguageMetadata(schema.language)
.headerCollectionPrefix;
const headerCollectionPrefix = getLanguageMetadata(
schema.language
).headerCollectionPrefix;

return {
...(headerCollectionPrefix && { headerCollectionPrefix }),
Expand Down Expand Up @@ -330,8 +335,8 @@ function transformObjectMapper(pipelineValue: PipelineValue) {
true /** immediateOnly */
);
const parentsRefs = immediateParents
.map(p => getMapperClassName(p))
.filter(p => p !== className);
.map((p) => getMapperClassName(p))
.filter((p) => p !== className);

const additionalProperties = buildAdditionalProperties(objectSchema);

Expand All @@ -350,12 +355,20 @@ function transformObjectMapper(pipelineValue: PipelineValue) {
// If any of the parents is present in uberParents we know it
// is its uber parent
let uberParent = getMapperClassName(
parents.find(p => uberParents.includes(getMapperClassName(p))) || schema
parents.find(
(p) =>
(p as ObjectSchema).discriminator &&
uberParents.includes(getMapperClassName(p))
) ||
parents.find((p) => uberParents.includes(getMapperClassName(p))) ||
schema
);

if (objectSchema.parents?.immediate[0]) {
uberParent = getMapperClassName(
objectSchema.parents?.immediate[0] as ObjectSchema
objectSchema.parents?.immediate.find(
(im) => (im as ObjectSchema).discriminator
) || (objectSchema.parents?.immediate[0] as ObjectSchema)
);
}
const mapper = buildMapper(
Expand Down Expand Up @@ -527,7 +540,7 @@ function transformChoiceMapper(pipelineValue: PipelineValue) {
} else {
type = {
name: MapperType.Enum,
allowedValues: choiceSchema.choices.map(choice => choice.value)
allowedValues: choiceSchema.choices.map((choice) => choice.value)
};
}
}
Expand Down Expand Up @@ -703,7 +716,7 @@ function processProperties(
options: EntityOptions = {}
) {
let modelProperties: ModelProperties = {};
properties.forEach(prop => {
properties.forEach((prop) => {
const serializedName = getPropertySerializedName(prop);
const propName = getLanguageMetadata(prop.language).name;
const name = normalizeName(
Expand Down Expand Up @@ -734,9 +747,9 @@ function getPropertySerializedName({
}

return flattenedNames
.map(name => {
.map((name) => {
// Escaping names
["."].forEach(character => {
["."].forEach((character) => {
name = name.replace(character, `\\${character}`);
});
return name;
Expand Down
39 changes: 26 additions & 13 deletions packages/autorest.typescript/src/transforms/objectTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export function transformObject(
description: metadata.description || undefined,
schema,
properties: schema.properties
? schema.properties.map((prop) =>
transformProperty(prop)
)
? schema.properties.map((prop) => transformProperty(prop))
: []
};

Expand Down Expand Up @@ -246,15 +244,28 @@ function transformPolymorphicObject(
let uberParent: ObjectSchema | undefined = objectDetails.schema;
const allParents = getSchemaParents(schema);
if (allParents && allParents.length) {
const uberParentSchema = allParents.find((p) => {
// TODO: Reconsider names to reduce issues with normalization, can we switch to serialized?
const name = normalizeName(
getLanguageMetadata(p.language).name,
NameType.Interface,
true /** shouldGuard */
);
return uberParents.some((up) => up.name === name);
});
const uberParentSchema =
allParents.find((p) => {
// TODO: Reconsider names to reduce issues with normalization, can we switch to serialized?
const name = normalizeName(
getLanguageMetadata(p.language).name,
NameType.Interface,
true /** shouldGuard */
);
return (
(p as ObjectSchema).discriminator &&
uberParents.some((up) => up.name === name)
);
}) ||
allParents.find((p) => {
// TODO: Reconsider names to reduce issues with normalization, can we switch to serialized?
const name = normalizeName(
getLanguageMetadata(p.language).name,
NameType.Interface,
true /** shouldGuard */
);
return uberParents.some((up) => up.name === name);
});

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

if (schema !== uberParent && objectDetails.schema.parents?.immediate[0]) {
uberParent = objectDetails.schema.parents?.immediate[0] as ObjectSchema;
uberParent = (objectDetails.schema.parents?.immediate.find(
(im) => (im as ObjectSchema).discriminator
) || objectDetails.schema.parents?.immediate[0]) as ObjectSchema;
}
const uberParentName = getLanguageMetadata(uberParent.language).name;
const unionName = `${normalizeName(uberParentName, NameType.Interface)}Union`;
Expand Down