Skip to content

Commit

Permalink
[v6] Naming fixes (#625)
Browse files Browse the repository at this point in the history
* Add reserved words and make operationFullName consistent

* Address pr comments

* Update naming utils and remove unecessary serializedNames
  • Loading branch information
joheredi authored Apr 28, 2020
1 parent adaeddd commit 8ae3cd5
Show file tree
Hide file tree
Showing 88 changed files with 728 additions and 1,209 deletions.
6 changes: 5 additions & 1 deletion src/generators/clientFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ function getOperationGroupsDeclarationDetails(
return operationGroups.map(og => {
return {
name: normalizeName(og.name, NameType.Property),
typeName: `operations.${normalizeName(og.name, NameType.Class)}`
typeName: `operations.${normalizeName(
og.name,
NameType.OperationGroup,
true /* shouldGuard */
)}`
};
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/generators/operationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ function addClass(
clientDetails: ClientDetails
) {
let importedModels = new Set<string>();
const className = normalizeName(operationGroupDetails.name, NameType.Class);
const className = normalizeName(
operationGroupDetails.name,
NameType.OperationGroup,
true /** shouldGuard */
);
const operationGroupClass = operationGroupFile.addClass({
name: className,
docs: [`Class representing a ${className}.`],
Expand Down
15 changes: 10 additions & 5 deletions src/transforms/mapperTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ export function transformMapper({ schema, options }: MapperInput) {
export function getMapperClassName(schema: Schema): string {
return normalizeName(
getLanguageMetadata(schema.language).name,
NameType.Class
NameType.Class,
true /** shouldGuard */
);
}

Expand Down Expand Up @@ -153,7 +154,8 @@ function buildMapper(
const serializedName =
getDiscriminatorValue(schema) ||
options.serializedName ||
getLanguageMetadata(schema.language).name;
// Fallback to name only for XML schemas since they need a name, otherwise don't
(options.hasXmlMetadata && getLanguageMetadata(schema.language).name);

const arraySchema = schema as ArraySchema;
arraySchema.elementType;
Expand Down Expand Up @@ -528,8 +530,7 @@ function transformConstantMapper(pipelineValue: PipelineValue): PipelineValue {
}
const serializedName =
(options && options.serializedName) ||
getLanguageMetadata(schema.language).serializedName ||
getLanguageMetadata(schema.language).name;
getLanguageMetadata(schema.language).serializedName;

const constantSchema = schema as ConstantSchema;

Expand Down Expand Up @@ -607,7 +608,11 @@ function processProperties(
properties.forEach(prop => {
const serializedName = getPropertySerializedName(prop);
const propName = getLanguageMetadata(prop.language).name;
const name = normalizeName(propName, NameType.Property);
const name = normalizeName(
propName,
NameType.Property,
true /** shouldGuard */
);
modelProperties[name] = getMapperOrRef(prop.schema, prop.serializedName, {
...options,
required: prop.required,
Expand Down
24 changes: 19 additions & 5 deletions src/transforms/objectTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export function transformObject(
uberParents: ObjectDetails[]
): ObjectDetails {
const metadata = getLanguageMetadata(schema.language);
let name = normalizeName(metadata.name, NameType.Class);
let name = normalizeName(
metadata.name,
NameType.Class,
true /** shouldGuard */
);
const kind = getObjectKind(schema);

let objectDetails: ObjectDetails = {
Expand Down Expand Up @@ -72,7 +76,11 @@ export function transformProperty({
const { typeName, isConstant, defaultValue } = typeDetails;

return {
name: normalizeName(metadata.name, NameType.Property),
name: normalizeName(
metadata.name,
NameType.Property,
true /** shouldGuard */
),
description: !metadata.description.startsWith("MISSING")
? metadata.description
: undefined,
Expand Down Expand Up @@ -144,7 +152,8 @@ function extractHierarchy(
.map(r => {
const relativeName = normalizeName(
getLanguageMetadata(r.language).name,
NameType.Interface
NameType.Interface,
true /** shouldGuard */
);
const relative = objectsDetails.find(o => o.name === relativeName);

Expand Down Expand Up @@ -188,7 +197,11 @@ function transformComposedObject(

const parentNames = schema.parents.immediate.map(parent => {
const name = getLanguageMetadata(parent.language).name;
return `${normalizeName(name, NameType.Interface)}`;
return `${normalizeName(
name,
NameType.Interface,
true /** shouldGuard */
)}`;
});

return {
Expand All @@ -209,7 +222,8 @@ function transformPolymorphicObject(
// TODO: Reconsider names to reduce issues with normalization, can we switch to serialized?
const name = normalizeName(
getLanguageMetadata(p.language).name,
NameType.Interface
NameType.Interface,
true /** shouldGuard */
);
return uberParents.some(up => up.name === name);
});
Expand Down
61 changes: 49 additions & 12 deletions src/transforms/operationTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
ConstantSchema,
CodeModel
} from "@azure-tools/codemodel";
import { normalizeName, NameType } from "../utils/nameUtils";
import {
normalizeName,
NameType,
getOperationFullName
} from "../utils/nameUtils";
import {
OperationGroupDetails,
OperationDetails,
Expand Down Expand Up @@ -86,7 +90,11 @@ export function transformOperationSpec(
operationDetails: OperationDetails,
parameters: ParameterDetails[]
): OperationSpecDetails[] {
const operationName = normalizeName(operationDetails.name, NameType.Property);
const operationName = normalizeName(
operationDetails.name,
NameType.Operation,
true /** shouldGuard */
);
// Extract protocol information
const operationFullName = operationDetails.fullName;

Expand Down Expand Up @@ -190,7 +198,11 @@ export function getSpecType(responseSchema: Schema, expand = false): SpecType {
break;
case SchemaType.Object:
const name = getLanguageMetadata(responseSchema.language).name;
reference = `Mappers.${normalizeName(name, NameType.Class)}`;
reference = `Mappers.${normalizeName(
name,
NameType.Class,
true /** shouldGuard */
)}`;
break;
default:
typeName = getMapperTypeFromSchema(responseSchema.type);
Expand Down Expand Up @@ -290,20 +302,33 @@ export function transformOperationResponse(

export async function transformOperation(
operation: Operation,
operationGroupName: string
operationGroup: OperationGroup,
clientName: string
): Promise<OperationDetails> {
const metadata = getLanguageMetadata(operation.language);
const pagination = extractPaginationDetails(operation);
const name = normalizeName(metadata.name, NameType.Property);
const operationFullName = `${operationGroupName}_${name}`;
const name = normalizeName(
metadata.name,
NameType.Operation,
true /** shouldGuard */
);
const operationFullName = getOperationFullName(
operationGroup,
operation,
clientName
);
const responsesAndErrors = [
...(operation.responses || []),
...(operation.exceptions || [])
];
const typeName = `${normalizeName(
operationGroupName,
getOperationGroupName(operationGroup, clientName),
NameType.Interface
)}${normalizeName(metadata.name, NameType.Interface)}`;
)}${normalizeName(
metadata.name,
NameType.Interface,
true /** shouldGuard */
)}`;

const typeDetails: TypeDetails = {
typeName,
Expand Down Expand Up @@ -347,7 +372,7 @@ export async function transformOperation(
return {
name,
typeDetails,
fullName: operationFullName.toLowerCase(),
fullName: operationFullName,
apiVersions: operation.apiVersions
? operation.apiVersions.map(v => v.version)
: [],
Expand All @@ -372,23 +397,35 @@ export function transformOperationGroups(
);
}

function getOperationGroupName(
operationGroup: OperationGroup,
clientName: string = ""
) {
const { name } = getLanguageMetadata(operationGroup.language);

return normalizeName(name || clientName, NameType.OperationGroup);
}

export async function transformOperationGroup(
operationGroup: OperationGroup,
clientName: string
): Promise<OperationGroupDetails> {
const metadata = getLanguageMetadata(operationGroup.language);
const isTopLevel = !metadata.name;
const name = normalizeName(metadata.name || clientName, NameType.Property);
const operationGroupClassName = getOperationGroupName(
operationGroup,
clientName
);

const operations = await Promise.all(
operationGroup.operations.map(operation =>
transformOperation(operation, name)
transformOperation(operation, operationGroup, clientName)
)
);
const mediaTypes = getOperationGroupMediaTypes(operations);

return {
name,
name: operationGroupClassName,
key: operationGroup.$key,
operations,
isTopLevel,
Expand Down
33 changes: 25 additions & 8 deletions src/transforms/parameterTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
} from "@azure-tools/codemodel";
import { QueryCollectionFormat } from "@azure/core-http";
import { getLanguageMetadata } from "../utils/languageHelpers";
import { normalizeName, NameType } from "../utils/nameUtils";
import {
normalizeName,
NameType,
getOperationFullName
} from "../utils/nameUtils";
import { ParameterDetails } from "../models/parameterDetails";
import {
isSchemaType,
Expand Down Expand Up @@ -111,13 +115,11 @@ export function transformParameters(
const extractOperationParameters = (codeModel: CodeModel) =>
codeModel.operationGroups.reduce<OperationParameterDetails[]>((acc, og) => {
const clientName = getLanguageMetadata(codeModel.language).name;
const groupName = getLanguageMetadata(og.language).name || clientName;
return [
...acc,
...og.operations.reduce<OperationParameterDetails[]>(
(operations, operation) => {
const opName = getLanguageMetadata(operation.language).name;
const operationName = `${groupName}_${opName}`.toLowerCase();
const operationName = getOperationFullName(og, operation, clientName);

// Look for request in old 'request' property if new 'requests' doesn't exist
const requests = operation.requests;
Expand Down Expand Up @@ -200,7 +202,11 @@ export function populateOperationParameters(
return;
}

const name = normalizeName(parameterName, NameType.Property);
const name = normalizeName(
parameterName,
NameType.Parameter,
true /** shouldGuard */
);
const serializedName =
getLanguageMetadata(parameter.language).serializedName || name;
const sameNameParams = operationParameters.filter(p => p.name === name);
Expand Down Expand Up @@ -275,12 +281,19 @@ function getIsGlobal(parameter: Parameter) {
function getParameterPath(parameter: Parameter) {
const metadata = getLanguageMetadata(parameter.language);
// ParameterPath has to include the name we used for the parameter, not the serializedName
const name = normalizeName(metadata.name, NameType.Property);
const name = normalizeName(
metadata.name,
NameType.Parameter,
true /** shouldGuard */
);

if (parameter.groupedBy) {
const groupedByName = getLanguageMetadata(parameter.groupedBy.language)
.name;
return [normalizeName(groupedByName, NameType.Property), name];
return [
normalizeName(groupedByName, NameType.Parameter, true /** shouldGuard */),
name
];
}

return isClientImplementation(parameter) || parameter.required
Expand Down Expand Up @@ -383,7 +396,11 @@ export function disambiguateParameter(
return;
} else {
// Since there is already a parameter with the same name, we need to ad a sufix
const name = normalizeName(serializedName, NameType.Property);
const name = normalizeName(
serializedName,
NameType.Parameter,
true /** shouldGuard */
);
const nameRef = `${name}${sameNameParams.length}`;
const collectionFormat = getCollectionFormat(parameter);
let description =
Expand Down
8 changes: 6 additions & 2 deletions src/transforms/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function transformChoice(
choice: ChoiceSchema | SealedChoiceSchema
): UnionDetails {
const metadata = getLanguageMetadata(choice.language);
let name = guardReservedNames(metadata.name);
let name = normalizeName(metadata.name, NameType.Interface);

return {
name,
Expand All @@ -57,7 +57,11 @@ export async function transformCodeModel(
codeModel: CodeModel,
host: Host
): Promise<ClientDetails> {
const className = normalizeName(codeModel.info.title, NameType.Class);
const className = normalizeName(
codeModel.info.title,
NameType.Class,
true /** shouldGuard */
);
normalizeModelWithExtensions(codeModel);

const [uberParents, operationGroups] = await Promise.all([
Expand Down
Loading

0 comments on commit 8ae3cd5

Please sign in to comment.