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

Fix the query datetime support issue #1884

Merged
merged 2 commits into from
Jun 13, 2023
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
8 changes: 6 additions & 2 deletions packages/typespec-ts/src/transform/transformParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,19 @@ function getParameterMetadata(
paramType: "query" | "path" | "header",
parameter: HttpOperationParameter
): ParameterMetadata {
const schemaContext = [SchemaContext.Exception, SchemaContext.Input];
const schema = getSchemaForType(
program,
dpgContext,
parameter.param.type,
[SchemaContext.Exception, SchemaContext.Input],
schemaContext,
false,
parameter.param
) as Schema;
let type = getTypeName(schema);
let type =
paramType === "query"
? getTypeName(schema, schemaContext)
: getTypeName(schema);
const name = getParameterName(parameter.name);
let description =
getFormattedPropertyDoc(program, parameter.param, schema) ?? "";
Expand Down
187 changes: 124 additions & 63 deletions packages/typespec-ts/test/unit/parametersGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,153 @@ import { emitParameterFromCadl } from "./util/emitUtil.js";
import { assertEqualContent } from "./util/testUtil.js";

describe("Parameters.ts", () => {
describe("handle query apiVersion", () => {
it("should't generate apiVersion if there's a client level apiVersion", async () => {
const parameters = await emitParameterFromCadl(
`
model ApiVersionParameter {
@query
"api-version": string;
}
op test(...ApiVersionParameter): string;
`
);
assert.ok(parameters);
assertEqualContent(
parameters?.content!,
`
import { RequestParameters } from "@azure-rest/core-client";
export type TestParameters = RequestParameters;
describe("query parameters", () => {
describe("apiVersion in query", () => {
it("should't generate apiVersion if there's a client level apiVersion", async () => {
const parameters = await emitParameterFromCadl(
`
);
model ApiVersionParameter {
@query
"api-version": string;
}
op test(...ApiVersionParameter): string;
`
);
assert.ok(parameters);
assertEqualContent(
parameters?.content!,
`
import { RequestParameters } from "@azure-rest/core-client";
export type TestParameters = RequestParameters;
`
);
});

it("should generate apiVersion if there's no client level apiVersion", async () => {
const parameters = await emitParameterFromCadl(
`
model ApiVersionParameter {
@query
"api-version": string;
}
op test(...ApiVersionParameter): string;
`,
false,
true
);
assert.ok(parameters);
assertEqualContent(
parameters?.content!,
`
import { RequestParameters } from "@azure-rest/core-client";
export interface TestQueryParamProperties {
"api-version": string;
}
export interface TestQueryParam {
queryParameters: TestQueryParamProperties;
}
export type TestParameters = TestQueryParam & RequestParameters;
`
);
});
});

it("should generate apiVersion if there's no client level apiVersion", async () => {
const parameters = await emitParameterFromCadl(
`
model ApiVersionParameter {
@query
"api-version": string;
}
op test(...ApiVersionParameter): string;
`,
false,
true
);
assert.ok(parameters);
assertEqualContent(
parameters?.content!,
`
describe("other parameters in query", () => {
it("should generate user-custom-query ", async () => {
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
const parameters = await emitParameterFromCadl(
`
model CustomParameter {
@query
"user-custom-query": string;
}
op test(...CustomParameter): string;
`
);
assert.ok(parameters);
assertEqualContent(
parameters?.content!,
`
import { RequestParameters } from "@azure-rest/core-client";
export interface TestQueryParamProperties {
"api-version": string;
"user-custom-query": string;
}
export interface TestQueryParam {
queryParameters: TestQueryParamProperties;
queryParameters: TestQueryParamProperties;
}
export type TestParameters = TestQueryParam & RequestParameters;
`
);
);
});

it("should generate offsetDateTime as Date | string", async () => {
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
const parameters = await emitParameterFromCadl(
`
model QueryParameter {
@query
executionTo?: offsetDateTime;
}
op test(...QueryParameter): string;
`
);
assert.ok(parameters);
assertEqualContent(
parameters?.content!,
`
import { RequestParameters } from "@azure-rest/core-client";
export interface TestQueryParamProperties {
executionTo?: Date | string;
}
export interface TestQueryParam {
queryParameters?: TestQueryParamProperties;
}
export type TestParameters = TestQueryParam & RequestParameters;
`
);
});
});
});

describe("query parameters generation", () => {
it("should generate user-custom-query ", async () => {
describe("header parameters", () => {
it("should generate offsetDateTime as string", async () => {
const parameters = await emitParameterFromCadl(
`
model CustomParameter {
@query
"user-custom-query": string;
}
op test(...CustomParameter): string;
`
model QueryParameter {
@header
executionTo?: offsetDateTime;
}
op test(...QueryParameter): string;
`
);
assert.ok(parameters);
assertEqualContent(
parameters?.content!,
`
import { RequestParameters } from "@azure-rest/core-client";
export interface TestQueryParamProperties {
"user-custom-query": string;
}
export interface TestQueryParam {
queryParameters: TestQueryParamProperties;
}
export type TestParameters = TestQueryParam & RequestParameters;
`
` import { RawHttpHeadersInput } from "@azure/core-rest-pipeline";
import { RequestParameters } from "@azure-rest/core-client";
export interface TestHeaders {
"execution-to"?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this different with what has been defined in the typespec ?

Copy link
Member Author

@MaryGao MaryGao Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timotheeguerin Hi, do you have any context on this ? The only document I can found about header is that they are case insensitive, but it doesn't say this dash mark can also be ignored. https://datatracker.ietf.org/doc/html/rfc7230#section-3.2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header decorator automatically convert camelCase to camel-case as the header value if you don’t specify an explicit value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this documented somewhere? I mean in TypeSpec documentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we did but I can't seem to find it. Will create an issue

}
export interface TestHeaderParam {
headers?: RawHttpHeadersInput & TestHeaders;
}
export type TestParameters = TestHeaderParam & RequestParameters;
`
);
});
});

describe("binary request generation", () => {
describe("binary request", () => {
it("bytes request with application/json will be treated as string", async () => {
const parameters = await emitParameterFromCadl(
`
Expand Down Expand Up @@ -294,7 +355,7 @@ describe("Parameters.ts", () => {
});
});

describe("Array in body generation", () => {
describe("array as request body", () => {
it("unknown array request generation", async () => {
const parameters = await emitParameterFromCadl(`
@post op read(@body body: unknown[]): void;
Expand Down Expand Up @@ -533,7 +594,7 @@ describe("Parameters.ts", () => {
});
});

describe("Dictionary in body generation", () => {
describe("dictionary as request body", () => {
it("Simple model dictionary request generation", async () => {
const parameters = await emitParameterFromCadl(`
model SimpleModel {
Expand Down