Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
msyyc committed Jan 15, 2025
2 parents dada044 + 1381733 commit d62e80a
Show file tree
Hide file tree
Showing 1,121 changed files with 112,647 additions and 3,057 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/generateSDK-2024-11-10-10-45-35.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- typespec-vscode
---

integrate client SDK generation
7 changes: 7 additions & 0 deletions .chronus/changes/generateSDK-2025-0-10-0-6-24.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/internal-build-utils"
---

resolve the program crash when there is no package name in package.json
7 changes: 7 additions & 0 deletions .chronus/changes/hide-codegen-config-2025-0-15-9-4-57.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- typespec-vscode
---

remove vscode settings for code generation
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/openapi3"
---

Adds support for @typespec/json-schema decorators with Open API 3.0 and 3.1 emitters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: feature
packages:
- "@typespec/openapi3"
---

Adds support for emitting Open API 3.1 models using the `openapi-versions` emitter configuration option.
Open API 3.0 is emitted by default.
7 changes: 7 additions & 0 deletions .chronus/changes/openapi31-playground-2025-0-9-12-38-34.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/playground"
---

Add support for displaying array-based emitter options
8 changes: 8 additions & 0 deletions .chronus/changes/scaffolding-emitter-2025-0-14-11-30-33.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
- typespec-vscode
---

Support Emitters section in Init Template when creating TypeSpec project in vscode
7 changes: 7 additions & 0 deletions .chronus/changes/wanl-fix-noway-yaml-2024-11-27-14-31-43.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Fix: OpenAPI YAML converts strings to boolean
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ packages/http-client-csharp/generator/TestProjects/**/tspCodeModel.json

# auto generated api view properties files
packages/http-client-java/generator/http-client-generator-test/src/main/**/*.json
packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/**/*.json
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ ignorePaths:
- "**/ThirdPartyNotices.txt"
- packages/compiler/test/formatter/scenarios/**
- packages/http-client-java/generator/http-client-generator-test/**
- packages/http-client-java/generator/http-client-generator-clientcore-test/**
- pnpm-lock.yaml
- "**/*.mp4"
- .git/**
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
import { TextDocument, TextEdit } from "vscode-languageserver-textdocument";
import type { CompilerHost, Program, SourceFile, TypeSpecScriptNode } from "../core/index.js";
import { LoadedCoreTemplates } from "../init/core-templates.js";
import { InitTemplate, InitTemplateLibrarySpec } from "../init/init-template.js";
import { EmitterTemplate, InitTemplate, InitTemplateLibrarySpec } from "../init/init-template.js";
import { ScaffoldingConfig } from "../init/scaffold.js";

export type ServerLogLevel = "trace" | "debug" | "info" | "warning" | "error";
Expand Down Expand Up @@ -172,3 +172,4 @@ export interface InitProjectContext {
export type InitProjectConfig = ScaffoldingConfig;
export type InitProjectTemplate = InitTemplate;
export type InitProjectTemplateLibrarySpec = InitTemplateLibrarySpec;
export type InitProjectTemplateEmitterTemplate = EmitterTemplate;
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function createModel(sdkContext: SdkContext<NetEmitterOptions>): CodeMode
Enums: Array.from(sdkTypeMap.enums.values()),
Models: Array.from(sdkTypeMap.models.values()),
Clients: inputClients,
Auth: processServiceAuthentication(sdkPackage),
Auth: processServiceAuthentication(sdkContext, sdkPackage),
};
return clientModel;

Expand Down
6 changes: 6 additions & 0 deletions packages/http-client-csharp/emitter/src/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const $lib = createTypeSpecLibrary({
"Cannot generate CSharp SDK since no public root client is defined in typespec file.",
},
},
"unsupported-auth": {
severity: "warning",
messages: {
default: paramMessage`${"message"}`,
},
},
},
emitter: {
options: NetEmitterOptionsSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

import {
SdkContext,
SdkCredentialParameter,
SdkCredentialType,
SdkHttpOperation,
SdkPackage,
} from "@azure-tools/typespec-client-generator-core";
import { NoTarget } from "@typespec/compiler";
import { Oauth2Auth, OAuth2Flow } from "@typespec/http";
import { NetEmitterOptions } from "../options.js";
import { InputAuth } from "../type/input-auth.js";
import { Logger } from "./logger.js";
import { reportDiagnostic } from "./lib.js";

export function processServiceAuthentication(
sdkContext: SdkContext<NetEmitterOptions>,
sdkPackage: SdkPackage<SdkHttpOperation>,
): InputAuth | undefined {
let authClientParameter: SdkCredentialParameter | undefined = undefined;
Expand All @@ -28,11 +32,11 @@ export function processServiceAuthentication(
return undefined;
}
if (authClientParameter.type.kind === "credential") {
return processAuthType(authClientParameter.type);
return processAuthType(sdkContext, authClientParameter.type);
}
const inputAuth: InputAuth = {};
for (const authType of authClientParameter.type.variantTypes) {
const auth = processAuthType(authType);
const auth = processAuthType(sdkContext, authType);
if (auth?.ApiKey) {
inputAuth.ApiKey = auth.ApiKey;
}
Expand All @@ -43,41 +47,61 @@ export function processServiceAuthentication(
return inputAuth;
}

function processAuthType(credentialType: SdkCredentialType): InputAuth | undefined {
function processAuthType(
sdkContext: SdkContext<NetEmitterOptions>,
credentialType: SdkCredentialType,
): InputAuth | undefined {
const scheme = credentialType.scheme;
switch (scheme.type) {
case "apiKey":
return { ApiKey: { Name: scheme.name } } as InputAuth;
if (scheme.in !== "header") {
reportDiagnostic(sdkContext.program, {
code: "unsupported-auth",
format: {
message: `Only header is supported for ApiKey authentication. ${scheme.in} is not supported.`,
},
target: credentialType.__raw ?? NoTarget,
});
return undefined;
}
return { ApiKey: { Name: scheme.name, In: scheme.in } } as InputAuth;
case "oauth2":
return processOAuth2(scheme);
case "http":
{
const schemeOrApiKeyPrefix = scheme.scheme;
switch (schemeOrApiKeyPrefix) {
case "basic":
Logger.getInstance().warn(
`${schemeOrApiKeyPrefix} auth method is currently not supported.`,
);
return undefined;
case "bearer":
return {
ApiKey: {
Name: "Authorization",
Prefix: "Bearer",
},
} as InputAuth;
default:
return {
ApiKey: {
Name: "Authorization",
Prefix: schemeOrApiKeyPrefix,
},
} as InputAuth;
}
case "http": {
const schemeOrApiKeyPrefix = scheme.scheme;
switch (schemeOrApiKeyPrefix) {
case "basic":
reportDiagnostic(sdkContext.program, {
code: "unsupported-auth",
format: { message: `${schemeOrApiKeyPrefix} auth method is currently not supported.` },
target: credentialType.__raw ?? NoTarget,
});
return undefined;
case "bearer":
return {
ApiKey: {
Name: "Authorization",
In: "header",
Prefix: "Bearer",
},
};
default:
return {
ApiKey: {
Name: "Authorization",
In: "header",
Prefix: schemeOrApiKeyPrefix,
},
};
}
break;
}
default:
throw new Error(`un-supported authentication scheme ${scheme.type}`);
reportDiagnostic(sdkContext.program, {
code: "unsupported-auth",
format: { message: `un-supported authentication scheme ${scheme.type}` },
target: credentialType.__raw ?? NoTarget,
});
return undefined;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

export interface InputApiKeyAuth {
Name: string;
In: ApiKeyLocation;
Prefix?: string;
}

export type ApiKeyLocation = "header"; // | "query" | "cookie"; // we do not support query or cookie yet
70 changes: 70 additions & 0 deletions packages/http-client-csharp/emitter/test/Unit/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { TestHost } from "@typespec/compiler/testing";
import { ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { createModel } from "../../src/lib/client-model-builder.js";
import {
createEmitterContext,
createEmitterTestHost,
createNetSdkContext,
typeSpecCompile,
} from "./utils/test-util.js";

describe("Test auth", () => {
let runner: TestHost;

beforeEach(async () => {
runner = await createEmitterTestHost();
});

it("cookie header is not supported", async () => {
const program = await typeSpecCompile(
`
op test(): NoContentResponse;
`,
runner,
{
AuthDecorator: `@useAuth(ApiKeyAuth<ApiKeyLocation.cookie, "api-key-name">)`,
},
);
const context = createEmitterContext(program);
const sdkContext = await createNetSdkContext(context);
const root = createModel(sdkContext);
const diagnostics = context.program.diagnostics;

const noAuthDiagnostic = diagnostics.find(
(d) => d.code === "@typespec/http-client-csharp/unsupported-auth",
);
ok(noAuthDiagnostic);
strictEqual(
noAuthDiagnostic.message,
"Only header is supported for ApiKey authentication. cookie is not supported.",
);
strictEqual(root.Auth, undefined); // we do not support it therefore it falls back to undefined
});

it("query header is not supported", async () => {
const program = await typeSpecCompile(
`
op test(): NoContentResponse;
`,
runner,
{
AuthDecorator: `@useAuth(ApiKeyAuth<ApiKeyLocation.query, "api-key-name">)`,
},
);
const context = createEmitterContext(program);
const sdkContext = await createNetSdkContext(context);
const root = createModel(sdkContext);
const diagnostics = context.program.diagnostics;

const noAuthDiagnostic = diagnostics.find(
(d) => d.code === "@typespec/http-client-csharp/unsupported-auth",
);
ok(noAuthDiagnostic);
strictEqual(
noAuthDiagnostic.message,
"Only header is supported for ApiKey authentication. query is not supported.",
);
strictEqual(root.Auth, undefined); // we do not support it therefore it falls back to undefined
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface TypeSpecCompileOptions {
IsAzureCoreNeeded?: boolean;
IsTCGCNeeded?: boolean;
IsXmlNeeded?: boolean;
AuthDecorator?: string;
}

export async function typeSpecCompile(
Expand All @@ -54,9 +55,11 @@ export async function typeSpecCompile(
const needAzureCore = options?.IsAzureCoreNeeded ?? false;
const needTCGC = options?.IsTCGCNeeded ?? false;
const needXml = options?.IsXmlNeeded ?? false;
const authDecorator =
options?.AuthDecorator ?? `@useAuth(ApiKeyAuth<ApiKeyLocation.header, "api-key">)`;
const namespace = `
@versioned(Versions)
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "api-key">)
${authDecorator}
@service({
title: "Azure Csharp emitter Testing",
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -26,7 +25,7 @@ namespace Microsoft.Generator.CSharp.ClientModel.Providers
/// <summary>
/// This class provides the set of serialization models, methods, and interfaces for a given model.
/// </summary>
internal class MrwSerializationTypeDefinition : TypeProvider
public class MrwSerializationTypeDefinition : TypeProvider
{
private const string JsonModelWriteCoreMethodName = "JsonModelWriteCore";
private const string JsonModelCreateCoreMethodName = "JsonModelCreateCore";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task ExecuteAsync()
var generatedTestOutputPath = CodeModelPlugin.Instance.Configuration.TestGeneratedDirectory;

GeneratedCodeWorkspace workspace = await GeneratedCodeWorkspace.Create();
await CodeModelPlugin.Instance.InitializeSourceInputModelAsync();
CodeModelPlugin.Instance.SourceInputModel = new SourceInputModel(await workspace.GetCompilationAsync());

var output = CodeModelPlugin.Instance.OutputLibrary;
Directory.CreateDirectory(Path.Combine(generatedSourceOutputPath, "Models"));
Expand Down
Loading

0 comments on commit d62e80a

Please sign in to comment.