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

Bump core from edf272d to 72d544c #1275

Merged
merged 7 commits into from
Aug 1, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-autorest"
---

Add support for encoding numeric types as string
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
changeKind: internal
packages:
- "@azure-tools/typespec-azure-core"
---

2 changes: 1 addition & 1 deletion core
Submodule core updated 159 files
8 changes: 4 additions & 4 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2180,12 +2180,12 @@ export async function getOpenAPIForService(
}
function mergeFormatAndEncoding(
format: string | undefined,
encoding: string,
encoding: string | undefined,
encodeAsFormat: string | undefined
): string {
): string | undefined {
switch (format) {
case undefined:
return encodeAsFormat ?? encoding;
return encodeAsFormat ?? encoding ?? format;
case "date-time":
switch (encoding) {
case "rfc3339":
Expand All @@ -2205,7 +2205,7 @@ export async function getOpenAPIForService(
return encodeAsFormat ?? encoding;
}
default:
return encodeAsFormat ?? encoding;
return encodeAsFormat ?? encoding ?? format;
}
}

Expand Down
23 changes: 21 additions & 2 deletions packages/typespec-autorest/test/primitive-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,16 @@ describe("typespec-autorest: primitives", () => {
async function testEncode(
scalar: string,
expectedOpenApi: OpenAPI2Schema,
encoding?: string,
encoding?: string | null,
encodeAs?: string
) {
const encodeAsParam = encodeAs ? `, ${encodeAs}` : "";
const encodeDecorator = encoding ? `@encode("${encoding}"${encodeAsParam})` : "";
const encodeDecorator =
encoding === null
? `@encode(${encodeAs})`
: encoding !== undefined
? `@encode("${encoding}"${encodeAsParam})`
: "";
const res1 = await oapiForModel("s", `${encodeDecorator} scalar s extends ${scalar};`);
deepStrictEqual(res1.defs.s, expectedOpenApi);
const res2 = await oapiForModel("Test", `model Test {${encodeDecorator} prop: ${scalar}};`);
Expand Down Expand Up @@ -287,5 +292,19 @@ describe("typespec-autorest: primitives", () => {
it("set format to base64url when encoding bytes as base64url", () =>
testEncode("bytes", { type: "string", format: "base64url" }, "base64url"));
});

describe("int64", () => {
it("set type: integer and format to 'int64' by default", () =>
testEncode("int64", { type: "integer", format: "int64" }));
it("set type: string and format to int64 when @encode(string)", () =>
testEncode("int64", { type: "string", format: "int64" }, null, "string"));
});

describe("decimal128", () => {
it("set type: integer and format to 'int64' by default", () =>
testEncode("decimal128", { type: "number", format: "decimal" }));
it("set type: string and format to int64 when @encode(string)", () =>
testEncode("decimal128", { type: "string", format: "decimal" }, null, "string"));
});
});
});
2 changes: 1 addition & 1 deletion packages/typespec-azure-core/src/rules/known-encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const knownEncodingRule = createRule({
create(context) {
function checkEncoding(type: ModelProperty | Scalar) {
const encode = getEncode(context.program, type);
if (encode) {
if (encode && encode.encoding) {
if (!knownEncodings.has(encode.encoding)) {
context.reportDiagnostic({
format: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ describe("typespec-client-generator-core: general decorators list", () => {
strictEqual(models.length, 1);
strictEqual(models[0].properties[0].decorators[0].name, "TypeSpec.@encode");
const encodeInfo = models[0].properties[0].decorators[0].arguments[
"encoding"
"encodingOrEncodeAs"
] as SdkEnumValueType;
strictEqual(encodeInfo.value, "base64url");
strictEqual((encodeInfo.value as any).value, "base64url");
expectDiagnostics(runner.context.diagnostics, []);
});

Expand Down
49 changes: 49 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading