Skip to content
Open
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
7,442 changes: 3,817 additions & 3,625 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/typespec
Submodule typespec updated 265 files
7 changes: 4 additions & 3 deletions src/typespec-aaz/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { SdkContext } from "@azure-tools/typespec-client-generator-core";
import { Program, Service, Tracer, TwoLevelMap, Type, TypeNameOptions } from "@typespec/compiler";
import { type TCGCContext } from "@azure-tools/typespec-client-generator-core";
import { Program, Service, Tracer, Type, TypeNameOptions } from "@typespec/compiler";
import { TwoLevelMap } from "@typespec/compiler/utils";
import { MetadataInfo, Visibility } from "@typespec/http";
import { PendingSchema, Ref } from "./model/schema.js";


export interface AAZEmitterContext {
readonly program: Program;
readonly service: Service;
readonly sdkContext: SdkContext;
readonly tcgcContext: TCGCContext;
readonly apiVersion: string;
tracer: Tracer
}
Expand Down
84 changes: 54 additions & 30 deletions src/typespec-aaz/src/convertor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { HttpOperation, HttpOperationBody, HttpOperationMultipartBody, HttpOperationResponse, HttpStatusCodeRange, HttpStatusCodesEntry, Visibility, createMetadataInfo, getHeaderFieldOptions, getQueryParamOptions, getServers, getStatusCodeDescription, getVisibilitySuffix, isContentTypeHeader, resolveRequestVisibility } from "@typespec/http";
import { HttpOperation, HttpOperationBody, HttpOperationMultipartBody, HttpOperationResponse, HttpStatusCodeRange, HttpStatusCodesEntry, Visibility, createMetadataInfo, getHeaderFieldOptions, getQueryParamOptions, getServers, getStatusCodeDescription, getVisibilitySuffix, resolveRequestVisibility, HttpProperty, } from "@typespec/http";
import {
isAzureResource,
} from "@azure-tools/typespec-azure-resource-manager";
import { AAZEmitterContext, AAZOperationEmitterContext, AAZSchemaEmitterContext } from "./context.js";
import { resolveOperationId, toCamelCase } from "./utils.js";
import { TypeSpecPathItem } from "./model/path_item.js";
import { CMDHttpOperation } from "./model/operation.js";
import { DiagnosticTarget, Enum, EnumMember, Model, ModelProperty, Namespace, Program, Scalar, TwoLevelMap, Type, Union, Value, getDiscriminator, getDoc, getEncode, getFormat, getMaxItems, getMaxLength, getMaxValue, getMaxValueExclusive, getMinItems, getMinLength, getMinValue, getMinValueExclusive, getPattern, getProjectedName, getProperty, isArrayModelType, isNeverType, isNullType, isRecordModelType, isService, isTemplateDeclaration, isVoidType, resolveEncodedName, IntrinsicType } from "@typespec/compiler";
import { DiagnosticTarget, Enum, EnumMember, Model, ModelProperty, Namespace, Program, Scalar, Type, Union, Value, getDiscriminator, getDoc, getEncode, getFormat, getMaxItems, getMaxLength, getMaxValue, getMaxValueExclusive, getMinItems, getMinLength, getMinValue, getMinValueExclusive, getPattern, getProperty, isArrayModelType, isNeverType, isNullType, isRecordModelType, isService, isTemplateDeclaration, isVoidType, resolveEncodedName, IntrinsicType } from "@typespec/compiler";
import { TwoLevelMap } from "@typespec/compiler/utils";
import { LroMetadata, PagedResultMetadata, UnionEnum, getArmResourceIdentifierConfig, getLroMetadata, getPagedResult, getUnionAsEnum } from "@azure-tools/typespec-azure-core";
import { XmsPageable } from "./model/x_ms_pageable.js";
import { CMDHttpRequest, CMDHttpResponse } from "./model/http.js";
Expand Down Expand Up @@ -191,30 +192,27 @@ function extractHttpRequest(context: AAZOperationEmitterContext, operation: Http
}

let clientRequestIdName;
for (const httpOpParam of methodParams.parameters) {
if (httpOpParam.type === "header" && isContentTypeHeader(context.program, httpOpParam.param)) {
for (const httpProperty of methodParams.properties) {
if (!["header", "query", "path"].includes(httpProperty.kind)) {
continue;
}
if (isNeverType(httpOpParam.param.type)) {
continue;
}
schemaContext = buildSchemaEmitterContext(context, httpOpParam.param, httpOpParam.type);
schemaContext = buildSchemaEmitterContext(context, httpProperty);
const schema = convert2CMDSchema(
schemaContext,
httpOpParam.param,
httpOpParam.name
httpProperty.property,
"options" in httpProperty ? httpProperty.options.name : ""
);
if (!schema) {
continue;
}

schema.required = !httpOpParam.param.optional;
schema.required = !httpProperty.property.optional;

if (paramModels[httpOpParam.type] === undefined) {
paramModels[httpOpParam.type] = {};
if (paramModels[httpProperty.kind] === undefined) {
paramModels[httpProperty.kind] = {};
}
paramModels[httpOpParam.type][schema.name] = schema;
if (httpOpParam.type === "header" && schema.name === "x-ms-client-request-id") {
paramModels[httpProperty.kind][schema.name] = schema;
if (httpProperty.kind === "header" && schema.name === "x-ms-client-request-id") {
clientRequestIdName = schema.name;
}
}
Expand Down Expand Up @@ -277,18 +275,22 @@ function extractHttpRequest(context: AAZOperationEmitterContext, operation: Http
let schema: CMDSchema | undefined;
if (body.property) {
context.tracer.trace("RetrieveBody", context.visibility.toString());
schemaContext = buildSchemaEmitterContext(context, body.property, "body");
schema = convert2CMDSchema(
schemaContext,
{
...context,
supportClsSchema: true,
},
body.property,
getJsonName(context, body.property)
)!;
schema.required = !body.property.optional;
} else {
schemaContext = buildSchemaEmitterContext(context, body.type, "body");
schema = {
...convert2CMDSchemaBase(
schemaContext,
{
...context,
supportClsSchema: true,
},
body.type
)!,
name: "body",
Expand Down Expand Up @@ -468,7 +470,8 @@ function convert2CMDHttpResponse(context: AAZOperationEmitterContext, response:
} else {
schema = convert2CMDSchemaBase(
{
...buildSchemaEmitterContext(context, body.type, "body"),
...context,
supportClsSchema: true,
visibility: Visibility.Read,
},
body.type
Expand All @@ -489,12 +492,36 @@ function convert2CMDHttpResponse(context: AAZOperationEmitterContext, response:

// Schema functions

function buildSchemaEmitterContext(context: AAZOperationEmitterContext, param: Type, type: "header" | "query" | "path" | "body" | "cookie"): AAZSchemaEmitterContext {
function getCollectionFormat(
context: AAZOperationEmitterContext,
type: ModelProperty,
explode?: boolean,
): "csv" | "ssv" | "pipes" | "multi" | undefined {
if (explode) {
return "multi";
}
const encode = getEncode(context.program, type);
if (encode) {
if (encode?.encoding === "ArrayEncoding.pipeDelimited") {
return "pipes";
}
if (encode?.encoding === "ArrayEncoding.spaceDelimited") {
return "ssv";
}
}
return "csv";
}

function buildSchemaEmitterContext(
context: AAZOperationEmitterContext,
httpProperty: HttpProperty,
): AAZSchemaEmitterContext {
let collectionFormat;
if (type === "query") {
collectionFormat = getQueryParamOptions(context.program, param).format;
} else if (type === "header") {
collectionFormat = getHeaderFieldOptions(context.program, param).format;
if (httpProperty.kind === "query") {
collectionFormat = getCollectionFormat(context, httpProperty.property, httpProperty.options.explode);
} else if (httpProperty.kind === "header") {
const headerOptions = getHeaderFieldOptions(context.program, httpProperty.property);
collectionFormat = getCollectionFormat(context, httpProperty.property, headerOptions.explode);
}
if (collectionFormat === "csv") {
collectionFormat = undefined;
Expand Down Expand Up @@ -1194,7 +1221,7 @@ function convertEnum2CMDSchemaBase(context: AAZSchemaEmitterContext, e: Enum): C
}

function shouldClientFlatten(context: AAZSchemaEmitterContext, target: ModelProperty): boolean {
return !!(shouldFlattenProperty(context.sdkContext, target) || getExtensions(context.program, target).get("x-ms-client-flatten"));
return !!(shouldFlattenProperty(context.tcgcContext, target) || getExtensions(context.program, target).get("x-ms-client-flatten"));
}

function includeDerivedModel(model: Model): boolean {
Expand Down Expand Up @@ -1705,11 +1732,8 @@ function applyExtensionsDecorators(
// Utils functions

function getJsonName(context: AAZOperationEmitterContext, type: Type & { name: string }): string {
const viaProjection = getProjectedName(context.program, type, "json");
const encodedName = resolveEncodedName(context.program, type, "application/json");
// Pick the value set via `encodedName` or default back to the legacy projection otherwise.
// `resolveEncodedName` will return the original name if no @encodedName so we have to do that check
return encodedName === type.name ? viaProjection ?? type.name : encodedName;
return encodedName === type.name ? type.name : encodedName;
}

function getPathWithoutQuery(path: string): string {
Expand Down
72 changes: 40 additions & 32 deletions src/typespec-aaz/src/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import {
emitFile,
ignoreDiagnostics,
listServices,
projectProgram,
Program,
resolvePath,
compilerAssert,
getService,
} from "@typespec/compiler";

import { buildVersionProjections } from "@typespec/versioning";
import { HttpService, getAllHttpServices, reportIfNoRoutes } from "@typespec/http";
import { unsafe_mutateSubgraphWithNamespace, } from "@typespec/compiler/experimental";
import { getVersioningMutators } from "@typespec/versioning";
import { HttpService, getHttpService, reportIfNoRoutes } from "@typespec/http";
import { getResourcePath, swaggerResourcePathToResourceId, } from "./utils.js";
import { AAZResourceSchema } from "./types.js";
import { AAZEmitterOptions, getTracer } from "./lib.js";
import { createSdkContext } from "@azure-tools/typespec-client-generator-core";
import { AAZEmitterOptions, getTracer, reportDiagnostic } from "./lib.js";
import { createTCGCContext } from "@azure-tools/typespec-client-generator-core";
import { AAZEmitterContext } from "./context.js";
import { retrieveAAZOperation } from "./convertor.js";

Expand Down Expand Up @@ -46,14 +47,15 @@ function createListResourceEmitter(context: EmitContext<AAZEmitterOptions>) {
services.push({ type: context.program.getGlobalNamespaceType() });
}
for (const service of services) {
const versions = buildVersionProjections(context.program, service.type);
for (const record of versions) {
let program = context.program;
if (record.projections.length > 0) {
program = projectProgram(context.program, record.projections);
}
const httpService = ignoreDiagnostics(getAllHttpServices(program))[0]
emitService(httpService, program, record.version!);
const versions = getVersioningMutators(context.program, service.type);
if (versions === undefined || versions.kind === "transient") {
continue;
}
for (const record of versions.snapshots) {
const subgraph = unsafe_mutateSubgraphWithNamespace(context.program, [record.mutator], service.type);
compilerAssert(subgraph.type.kind === "Namespace", "Should not have mutated to another type");
const httpService = ignoreDiagnostics(getHttpService(context.program, (getService(context.program, subgraph.type) || service).type));
emitService(httpService, context.program, record.version?.value);
}
}

Expand Down Expand Up @@ -84,13 +86,12 @@ function createListResourceEmitter(context: EmitContext<AAZEmitterOptions>) {
}

async function createGetResourceOperationEmitter(context: EmitContext<AAZEmitterOptions>) {
const sdkContext = await createSdkContext(context, "@azure-tools/typespec-aaz");
const tcgcContext = createTCGCContext(context.program, "@azure-tools/typespec-aaz");
const tracer = getTracer(context.program);
tracer.trace("options", JSON.stringify(context.options, null, 2));
const apiVersion = sdkContext.apiVersion!;
const apiVersion = context.options["api-version"]!;
tracer.trace("apiVersion", apiVersion);

let projectedProgram: Program;
const resOps: Record<string, AAZResourceSchema> = {};
context.options?.resources?.forEach((id) => {
resOps[id] = {
Expand All @@ -102,20 +103,27 @@ async function createGetResourceOperationEmitter(context: EmitContext<AAZEmitter

async function getResourcesOperations() {
const services = listServices(context.program);
if (services.length === 0) {
services.push({ type: context.program.getGlobalNamespaceType() });
}
for (const service of services) {
// currentService = service;
const versions = buildVersionProjections(context.program, service.type).filter(
(v) => apiVersion === v.version
);
for (const record of versions) {
projectedProgram = context.program;
if (record.projections.length > 0) {
projectedProgram = projectProgram(context.program, record.projections);
}
const versions = getVersioningMutators(context.program, service.type);
if (versions === undefined || versions.kind === "transient") {
reportDiagnostic(context.program, {
code: "invalid-program-versions",
target: service.type,
});
continue;
}
const filteredVersions = versions.snapshots.filter((v) => apiVersion === v.version?.value);
for (const record of filteredVersions) {
const subgraph = unsafe_mutateSubgraphWithNamespace(context.program, [record.mutator], service.type);
compilerAssert(subgraph.type.kind === "Namespace", "Should not have mutated to another type");
const aazContext: AAZEmitterContext = {
program: projectedProgram,
service: service,
sdkContext: sdkContext,
program: context.program,
service: getService(context.program, subgraph.type) || service,
tcgcContext: tcgcContext,
apiVersion: apiVersion,
tracer,
};
Expand All @@ -135,11 +143,11 @@ async function createGetResourceOperationEmitter(context: EmitContext<AAZEmitter
return { getResourcesOperations };

function emitResourceOps(context: AAZEmitterContext) {
const services = ignoreDiagnostics(getAllHttpServices(context.program));
const operations = services[0].operations;
reportIfNoRoutes(projectedProgram, operations);
const httpService = ignoreDiagnostics(getHttpService(context.program, context.service.type));
const routes = httpService.operations;
reportIfNoRoutes(context.program, routes);

operations.forEach((op) => {
routes.forEach((op) => {
const resourcePath = getResourcePath(context.program, op);
const resourceId = swaggerResourcePathToResourceId(resourcePath);
if (resourceId in resOps) {
Expand Down
6 changes: 6 additions & 0 deletions src/typespec-aaz/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const libDef = {
default: "Missing status codes",
}
},
"invalid-program-versions": {
severity: "error",
messages: {
default: "Invalid service versions from program",
}
},
"duplicate-body-types": {
severity: "error",
messages: {
Expand Down
7 changes: 3 additions & 4 deletions src/typespec-aaz/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Program, Type, getProjectedName, isGlobalNamespace, isService } from "@typespec/compiler";
import { Program, Type, isGlobalNamespace, isService } from "@typespec/compiler";
import { HttpOperation, isSharedRoute } from "@typespec/http";
import { getOperationId } from "@typespec/openapi";
import { pascalCase } from "change-case";
Expand Down Expand Up @@ -59,9 +59,8 @@ export function getResourcePath(program: Program, operation: HttpOperation) {
}

function getAutorestClientName(context: AAZEmitterContext, type: Type & { name: string }) {
const viaProjection = getProjectedName(context.program, type, "client");
const clientName = getClientNameOverride(context.sdkContext, type);
return clientName ?? viaProjection ?? type.name;
const clientName = getClientNameOverride(context.tcgcContext, type);
return clientName ?? type.name;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/web/src/typespec/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {
CompilerHost,
// CompilerOptions,
LinterDefinition,
NodePackage,
PackageJson,
TypeSpecLibrary,
} from "@typespec/compiler";

export interface TspLibrary {
name: string;
packageJson: NodePackage;
packageJson: PackageJson;
isEmitter: boolean;
definition?: TypeSpecLibrary<any>;
linter?: LinterDefinition;
Expand Down