Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support client request id modular and small fixes #1985

Merged
merged 45 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
16ceb57
Ignore the custom client-request-id header in modular
MaryGao Aug 20, 2023
677efb1
Remove the only item
MaryGao Aug 21, 2023
3ac93fa
Remove the debug options
MaryGao Aug 21, 2023
3944b0f
Change test case namings
MaryGao Aug 21, 2023
16a80f8
Revert changes
MaryGao Aug 21, 2023
68e8340
update changes
MaryGao Aug 21, 2023
d897e77
Merge branch 'main' into client-request-id-modular
MaryGao Aug 22, 2023
59548b0
update the test cases
MaryGao Aug 22, 2023
5e1b7ee
Merge remote-tracking branch 'origin/main' into client-request-id-mod…
MaryGao Aug 22, 2023
8fdbb1f
Merge remote-tracking branch 'origin/main' into client-request-id-mod…
MaryGao Aug 24, 2023
44d5dd2
update the code
MaryGao Aug 24, 2023
be95960
update the code to fix modular issue
MaryGao Aug 24, 2023
017ec66
Regen smoke and integration testing
MaryGao Aug 24, 2023
4b0e74f
update test cases
MaryGao Aug 24, 2023
68db99b
update the test cases
MaryGao Aug 24, 2023
8d5962d
update the isunexpected helper
MaryGao Aug 24, 2023
3294acf
Fix int test issues
MaryGao Aug 24, 2023
c6ce099
Fix the test issue
MaryGao Aug 24, 2023
077d4f0
Fix this test issue
MaryGao Aug 24, 2023
bac92fb
update the test cases
MaryGao Aug 25, 2023
a4549e9
Add more ignored headers
MaryGao Aug 25, 2023
ea5195d
Refactoring
MaryGao Aug 25, 2023
a0a3c2a
Enable azure core cases
MaryGao Aug 25, 2023
4037c4f
adjust the positions
MaryGao Aug 25, 2023
a16d3e7
Update .vscode/launch.json
MaryGao Aug 25, 2023
bde19ee
Merge branch 'client-request-id-modular' of https://github.com/maryga…
MaryGao Aug 25, 2023
7d02c17
fix the issue bugs
MaryGao Aug 25, 2023
ab002b2
Update packages/typespec-ts/src/index.ts
MaryGao Aug 25, 2023
6d51dd3
Fix small issue
MaryGao Aug 25, 2023
f461f96
Remove useless codes
MaryGao Aug 25, 2023
a4c8e09
Merge remote-tracking branch 'origin/main' into client-request-id-mod…
MaryGao Aug 28, 2023
6500a92
Revert the changes for default response support
MaryGao Aug 28, 2023
b2fd363
Regenerate codes
MaryGao Aug 28, 2023
41b0c92
Revert changes in rlc int test
MaryGao Aug 28, 2023
b44a24d
Remove console.log code lines
MaryGao Aug 28, 2023
25c7763
Update packages/typespec-ts/test/integration/encodeDatetime.spec.ts
MaryGao Aug 28, 2023
b41209f
Update packages/typespec-ts/test/integration/encodeDatetime.spec.ts
MaryGao Aug 28, 2023
5f2ff58
Update packages/typespec-ts/test/integration/encodeDatetime.spec.ts
MaryGao Aug 28, 2023
a89d16d
Update packages/typespec-ts/test/integration/encodeDatetime.spec.ts
MaryGao Aug 28, 2023
e4ed62a
Update packages/typespec-ts/test/integration/encodeDatetime.spec.ts
MaryGao Aug 28, 2023
3e57587
Revert changes
MaryGao Aug 28, 2023
ced9263
fix lint issues
MaryGao Aug 28, 2023
75e49d1
Revert changes
MaryGao Aug 28, 2023
16b1342
Update packages/typespec-ts/src/modular/helpers/operationHelpers.ts
MaryGao Aug 29, 2023
7890b6b
update the order
MaryGao Aug 29, 2023
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
46 changes: 36 additions & 10 deletions packages/rlc-common/src/buildObjectTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export function buildObjectInterfaces(
const objectInterfaces: InterfaceDeclarationStructure[] = [];

for (const objectSchema of objectSchemas) {
if (objectSchema.alias || objectSchema.outputAlias) {
if (
objectSchema.alias ||
objectSchema.outputAlias ||
objectSchema.fromCore
) {
continue;
}
const baseName = getObjectBaseName(objectSchema, schemaUsage);
Expand Down Expand Up @@ -417,18 +421,40 @@ export function getPropertySignature(
importedModels: Set<string>
): PropertySignatureStructure {
const propertyName = property.name;

const description = property.description;
let type =
generateForOutput(schemaUsage, property.usage) && property.outputTypeName
? property.outputTypeName
: property.typeName
? property.typeName
: property.type;
if (property.typeName && property.fromCore) {
importedModels.add(property.typeName);
let type;
const hasCoreInArray =
property.type === "array" &&
(property as any).items &&
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
(property as any).items.fromCore;
const hasCoreInRecord =
property.type === "dictionary" &&
(property as any).additionalProperties &&
(property as any).additionalProperties.fromCore;
if (hasCoreInArray && property.typeName) {
type = property.typeName;
importedModels.add(
(property as any).items.typeName ?? (property as any).items.name
);
} else if (hasCoreInRecord && property.typeName) {
type = property.typeName;
importedModels.add(
(property as any).additionalProperties.typeName ??
(property as any).additionalProperties.name
);
} else {
type =
generateForOutput(schemaUsage, property.usage) && property.outputTypeName
? property.outputTypeName
: property.typeName
? property.typeName
: property.type;
if (property.typeName && property.fromCore) {
importedModels.add(property.typeName);
type = property.typeName;
}
}

return {
name: propertyName,
...(description && { docs: [{ description }] }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { Client } from '@azure-rest/core-client';
import { ClientOptions } from '@azure-rest/core-client';
import { CreateHttpPollerOptions } from '@azure/core-lro';
import { ErrorModel } from '@azure-rest/core-client';
import { ErrorResponse } from '@azure-rest/core-client';
import { HttpResponse } from '@azure-rest/core-client';
import { KeyCredential } from '@azure/core-auth';
Expand Down Expand Up @@ -234,15 +235,6 @@ export interface DocumentContent {
value: string;
}

// @public
export interface ErrorModelOutput {
code: string;
details?: Array<ErrorModelOutput>;
innererror?: InnerErrorOutput;
message: string;
target?: string;
}

// @public
export interface ExtendedClinicalCodedElementOutput {
category?: string;
Expand Down Expand Up @@ -306,12 +298,6 @@ export type HealthInsightsClinicalMatchingClient = Client & {
path: Routes;
};

// @public
export interface InnerErrorOutput {
code?: string;
innererror?: InnerErrorOutput;
}

// @public (undocumented)
export function isUnexpected(response: GetJob200Response | GetJobDefaultResponse): response is GetJobDefaultResponse;

Expand Down Expand Up @@ -391,7 +377,7 @@ export interface TrialMatcherPatientResultOutput {
// @public
export interface TrialMatcherResultOutput {
readonly createdDateTime: string;
readonly errors?: Array<ErrorModelOutput>;
readonly errors?: Array<ErrorModel>;
readonly expirationDateTime: string;
readonly jobId: string;
readonly lastUpdateDateTime: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ErrorModel } from "@azure-rest/core-client";

/** The response for the Trial Matcher request. */
export interface TrialMatcherResultOutput {
/** A processing job identifier. */
Expand All @@ -18,33 +20,11 @@ export interface TrialMatcherResultOutput {
*/
readonly status: string;
/** An array of errors, if any errors occurred during the processing job. */
readonly errors?: Array<ErrorModelOutput>;
readonly errors?: Array<ErrorModel>;
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
/** The inference results for the Trial Matcher request. */
readonly results?: TrialMatcherResultsOutput;
}

/** The error object. */
export interface ErrorModelOutput {
/** One of a server-defined set of error codes. */
code: string;
/** A human-readable representation of the error. */
message: string;
/** The target of the error. */
target?: string;
/** An array of details about specific errors that led to this reported error. */
details?: Array<ErrorModelOutput>;
/** An object containing more specific information than the current object about the error. */
innererror?: InnerErrorOutput;
}

/** An object containing more specific information about the error. As per Microsoft One API guidelines - https://github.com/Microsoft/api-guidelines/blob/vNext/Guidelines.md#7102-error-condition-responses. */
export interface InnerErrorOutput {
/** One of a server-defined set of error codes. */
code?: string;
/** Inner error. */
innererror?: InnerErrorOutput;
}

/** The inference results for the Trial Matcher request. */
export interface TrialMatcherResultsOutput {
/** Results for the patients given in the request. */
Expand Down
10 changes: 6 additions & 4 deletions packages/typespec-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { join } from "path";
import { GenerationDirDetail, SdkContext } from "./utils/interfaces.js";
import { transformRLCOptions } from "./transform/transfromRLCOptions.js";
import { ModularCodeModel } from "./modular/modularCodeModel.js";
import { getClientName } from "@azure-tools/rlc-common";

export * from "./lib.js";

Expand Down Expand Up @@ -119,7 +120,10 @@ export async function $onEmit(context: EmitContext) {
const rlcModels = await transformRLCModel(client, dpgContext);
rlcCodeModels.push(rlcModels);
serviceNameToRlcModelsMap.set(client.service.name, rlcModels);
needUnexpectedHelper.set(client.name, hasUnexpectedHelper(rlcModels));
needUnexpectedHelper.set(
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
getClientName(rlcModels),
hasUnexpectedHelper(rlcModels)
);

await emitModels(rlcModels, program);
await emitContentByBuilder(program, buildClientDefinitions, rlcModels);
Expand Down Expand Up @@ -162,9 +166,7 @@ export async function $onEmit(context: EmitContext) {
buildModels(modularCodeModel, subClient);
buildModelsOptions(modularCodeModel, subClient);
const hasClientUnexpectedHelper =
needUnexpectedHelper.get(
subClient.rlcClientName.replace("Context", "Client")
) ?? false;
needUnexpectedHelper.get(subClient.rlcClientName) ?? false;
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
buildOperationFiles(
dpgContext,
modularCodeModel,
Expand Down
32 changes: 11 additions & 21 deletions packages/typespec-ts/src/modular/buildCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
import {
getAuthentication,
getHeaderFieldName,
getHttpOperation,
getPathParamName,
getQueryParamName,
getServers,
Expand All @@ -53,7 +52,8 @@ import {
HttpServer,
isStatusCode,
HttpOperation,
isHeader
isHeader,
getHttpOperation
} from "@typespec/http";
import { getAddedOnVersions } from "@typespec/versioning";
import {
Expand All @@ -80,7 +80,10 @@ import {
Type as HrlcType,
Header
} from "./modularCodeModel.js";
import { getEnrichedDefaultApiVersion } from "../utils/modelUtils.js";
import {
getEnrichedDefaultApiVersion,
isAzureCoreErrorType
} from "../utils/modelUtils.js";
import { camelToSnakeCase, toCamelCase } from "../utils/casingUtils.js";
import {
RLCModel,
Expand All @@ -90,7 +93,8 @@ import {
} from "@azure-tools/rlc-common";
import {
getOperationGroupName,
getOperationName
getOperationName,
isIgnoredHeaderParam
} from "../utils/operationUtil.js";
import { SdkContext } from "../utils/interfaces.js";
import { Project } from "ts-morph";
Expand Down Expand Up @@ -554,23 +558,6 @@ function emitResponseHeaders(
return retval;
}

function isAzureCoreErrorType(t?: Type): boolean {
if (
t?.kind !== "Model" ||
!["Error", "ErrorResponse", "InnerError"].includes(t.name)
)
return false;
const namespaces = ".Azure.Core.Foundations".split(".");
while (
namespaces.length > 0 &&
(t?.kind === "Model" || t?.kind === "Namespace") &&
t.namespace?.name === namespaces.pop()
) {
t = t.namespace;
}
return namespaces.length == 0;
}

function emitResponse(
context: SdkContext,
response: HttpOperationResponse,
Expand Down Expand Up @@ -753,6 +740,9 @@ function emitBasicOperation(
});

for (const param of httpOperation.parameters.parameters) {
if (isIgnoredHeaderParam(param)) {
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
const emittedParam = emitParameter(context, param, "Method");
if (isApiVersion(context, param) && apiVersionParam === undefined) {
apiVersionParam = emittedParam;
Expand Down
13 changes: 8 additions & 5 deletions packages/typespec-ts/src/modular/emitModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function buildModels(
// We are generating both models and enums here
const coreClientTypes = new Set<string>();
const models = codeModel.types.filter(
(t) => (t.type === "model" || t.type === "enum") && !isAzureCoreError(t)
(t) =>
(t.type === "model" || t.type === "enum") && !isAzureCoreErrorSdkType(t)
);

// Skip to generate models.ts if there is no any models
Expand Down Expand Up @@ -78,8 +79,8 @@ export function buildModels(
properties: properties.map((p) => {
const propertyMetadata = getType(p.type);
let propertyTypeName = propertyMetadata.name;
if (isAzureCoreError(p.type)) {
propertyTypeName = isAzureCoreError(p.type)
if (isAzureCoreErrorSdkType(p.type)) {
propertyTypeName = isAzureCoreErrorSdkType(p.type)
? getCoreClientErrorType(propertyTypeName)
: propertyTypeName;
}
Expand Down Expand Up @@ -122,10 +123,12 @@ export function buildModels(
return modelsFile;
}

function isAzureCoreError(t: Type) {
function isAzureCoreErrorSdkType(t: Type) {
return (
t.name &&
["Error", "InnerError"].includes(t.name) &&
["error", "errormodel", "innererror", "errorresponse"].includes(
t.name.toLowerCase()
) &&
t.isCoreErrorType === true
);
}
Expand Down
28 changes: 19 additions & 9 deletions packages/typespec-ts/src/modular/helpers/operationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function getDeserializePrivateFunction(
): OptionalKind<FunctionDeclarationStructure> {
const { name } = getOperationName(operation);

let parameters: OptionalKind<ParameterDeclarationStructure>[] = [
const parameters: OptionalKind<ParameterDeclarationStructure>[] = [
{
name: "result",
type: getRLCResponseType(operation.rlcResponse)
Expand All @@ -103,14 +103,6 @@ export function getDeserializePrivateFunction(
if (response?.type?.type) {
returnType = buildType(response.type.name, response.type);
} else {
if (!needUnexpectedHelper) {
parameters = [
{
name: "_result",
type: getRLCResponseType(operation.rlcResponse)
}
];
}
returnType = { name: "", type: "void" };
}

Expand All @@ -128,6 +120,24 @@ export function getDeserializePrivateFunction(
"throw result.body",
"}"
);
} else {
const validStatus = [
...new Set(
operation.responses
.flatMap((r) => r.statusCodes)
.filter((s) => s !== "default")
)
];

if (validStatus.length > 0) {
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
statements.push(
`if(${validStatus
.map((s) => `result.status !== "${s}"`)
.join(" || ")}){`,
"throw result.body",
"}"
);
}
}

if (response?.type?.type === "any") {
Expand Down
6 changes: 2 additions & 4 deletions packages/typespec-ts/src/transform/transformResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,10 @@ function transformBody(
const bodySchema = getSchemaForType(dpgContext, body!.type, [
SchemaContext.Output
]) as Schema;
if (bodySchema.fromCore) {
fromCore = true;
}
fromCore = bodySchema.fromCore ?? false;
const bodyType = getTypeName(bodySchema);
const importedNames = getImportedModelName(bodySchema);
if (importedNames) {
if (importedNames && !fromCore) {
importedNames
.filter((name) => {
return name !== "any";
Expand Down
10 changes: 3 additions & 7 deletions packages/typespec-ts/src/transform/transformSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
getSchemaForType,
includeDerivedModel,
getBodyType,
trimUsage
trimUsage,
isAzureCoreErrorType
} from "../utils/modelUtils.js";
import { SdkContext } from "../utils/interfaces.js";

Expand Down Expand Up @@ -61,12 +62,7 @@ export function transformSchemas(
getGeneratedModels(bodyModel, SchemaContext.Input);
}
for (const resp of route.responses) {
if (
resp.type.kind === "Model" &&
resp.type.name === "ErrorResponse" &&
resp.type.namespace?.name === "Foundations" &&
resp.type.namespace.namespace?.name === "Core"
) {
if (isAzureCoreErrorType(resp.type)) {
continue;
}
for (const resps of resp.responses) {
Expand Down
Loading
Loading