Skip to content
Draft
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"
---

Honor inherited Azure Core API-version overrides in emitted OpenAPI documents and warn when a document has inconsistent overrides.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Add a `version` option to `@featureFileOptions` for overriding the generated client API version.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-go"
---

Generate Go clients with the opaque API-version default configured for each client.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-azure-core"
---

Add the legacy `@Azure.Core.Legacy.overrideApiVersion` decorator for overriding inherited and
optionally language-scoped API-version wire defaults on namespaces and interfaces.

```typespec
@Azure.Core.Legacy.overrideApiVersion("2021-11-01")
interface Widgets {
get(): void;
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Populate per-client API-version defaults from `Azure.Core.Legacy.overrideApiVersion` for downstream emitters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-ts"
---

Generate TypeScript clients with the opaque API-version default configured for each client.
6 changes: 6 additions & 0 deletions packages/typespec-autorest/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ export const $lib = createTypeSpecLibrary({
"Cannot emit service.yaml because the project defines multiple services. Only the first service will be included.",
},
},
"inconsistent-client-api-version-override": {
severity: "warning",
messages: {
default: paramMessage`Operations emitted to the same OpenAPI document must specify one consistent \`@overrideApiVersion\` value. Found values: ${"values"}. The normal document version ${"fallback"} will be retained.`,
},
},
},
emitter: {
options: EmitterOptionsSchema as JSONSchemaType<AutorestEmitterOptions>,
Expand Down
59 changes: 56 additions & 3 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
extractLroStates,
getArmResourceIdentifierConfig,
getAsEmbeddingVector,
getEffectiveApiVersionOverride,
getLroMetadata,
getUnionAsEnum,
hasUniqueItems,
} from "@azure-tools/typespec-azure-core";
import {
type ArmFeatureOptions,
type ArmFeatureFileOptions,
getArmCommonTypeOpenAPIRef,
getArmIdentifiers,
getArmKeyIdentifiers,
Expand Down Expand Up @@ -3035,6 +3036,7 @@ export function createDefaultDocumentProxy(
const definitions = new Map<string, OpenAPI2Schema>();
const parameters: Map<string, [ModelProperty, OpenAPI2Parameter]> = new Map();
const operationIds = new DuplicateTracker<string, Operation>();
const operations: HttpOperation[] = [];
let examples: Map<string, Record<string, LoadedExample>> = new Map();
let operationIdsWithExamples: Set<string> = new Set();
return {
Expand Down Expand Up @@ -3066,6 +3068,7 @@ export function createDefaultDocumentProxy(
},

createOrGetEndpoint(op: HttpOperation, context: AutorestEmitterContext): OpenAPI2Operation {
operations.push(op);
const pathItem = initPathItem(program, op, root);
if (!pathItem[op.verb]) {
pathItem[op.verb] = { parameters: [] };
Expand Down Expand Up @@ -3109,6 +3112,7 @@ export function createDefaultDocumentProxy(
},
resolveDocuments(context: AutorestEmitterContext) {
reportDuplicateOperationIds(program, operationIds);
applyClientApiVersionOverride(root, operations, context, service.type);
root.definitions = {};
for (const [name, schema] of definitions) {
root.definitions[name] = schema;
Expand Down Expand Up @@ -3158,8 +3162,9 @@ export function createDefaultDocumentProxy(
interface OpenAPI2DocumentItem {
document: OpenAPI2Document;
operationExamples: Map<string, LoadedExample[]>;
operations: HttpOperation[];
tags: Set<string>;
options: ArmFeatureOptions;
options: ArmFeatureFileOptions;
}

function createFeatureDocumentProxy(
Expand Down Expand Up @@ -3221,6 +3226,7 @@ function createFeatureDocumentProxy(
createOrGetEndpoint(op: HttpOperation, context: AutorestEmitterContext): OpenAPI2Operation {
const options = getFeature(program, op.operation);
const item = root.get(options.featureName.toLowerCase())!;
item.operations.push(op);
const pathItem = initPathItem(program, op, item.document);
if (!pathItem[op.verb]) {
pathItem[op.verb] = { parameters: [] };
Expand Down Expand Up @@ -3281,6 +3287,13 @@ function createFeatureDocumentProxy(
reportDuplicateOperationIds(program, tracker);
}
for (const [featureName, featureItem] of root.entries()) {
applyClientApiVersionOverride(
featureItem.document,
featureItem.operations,
context,
service.type,
featureItem.options.version,
);
const exampleIds = operationFeatures.get(featureName) || new Set<string>();
const featureExamples = [...exampleIds]
.filter((id) => operationIdsWithExamples.has(id))
Expand Down Expand Up @@ -3377,17 +3390,57 @@ function reportDuplicateOperationIds(
function initializeOpenAPIDocumentItem(
program: Program,
service: Service,
options: ArmFeatureOptions,
options: ArmFeatureFileOptions,
version?: string,
): OpenAPI2DocumentItem {
return {
document: initializeOpenApi2Document(program, service, version),
operationExamples: new Map<string, LoadedExample[]>(),
operations: [],
tags: new Set<string>(),
options,
};
}

function applyClientApiVersionOverride(
document: OpenAPI2Document,
operations: HttpOperation[],
context: AutorestEmitterContext,
diagnosticTarget: Namespace,
explicitVersion?: string,
): void {
if (explicitVersion !== undefined) {
document.info.version = explicitVersion;
return;
}
if (operations.length === 0) return;

const overrides = operations.map((operation) =>
getEffectiveApiVersionOverride(
context.program,
operation.operation,
"@azure-tools/typespec-autorest",
),
);
if (overrides.every((value) => value === undefined)) return;

const first = overrides[0];
if (first !== undefined && overrides.every((value) => value === first)) {
document.info.version = first;
return;
}

const values = [...new Set(overrides.map((value) => value ?? "<none>"))];
reportDiagnostic(context.program, {
code: "inconsistent-client-api-version-override",
format: {
values: values.join(", "),
fallback: document.info.version,
},
target: diagnosticTarget,
});
}

function initializeOpenApi2Document(
program: Program,
service: Service,
Expand Down
Loading
Loading