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

Remove reference to autorest from arm library #811

Merged
merged 10 commits into from
May 9, 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,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Remove dependency on `typespec-autorest` emitter
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@azure-tools/typespec-autorest"
---

Add dependency on typespec-azure-resource-manager to resolve the spec repo common types paths
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.Versioning;
using Azure.ResourceManager;
using Autorest;

@armProviderNamespace
@service({
Expand Down
2 changes: 2 additions & 0 deletions packages/typespec-autorest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
],
"peerDependencies": {
"@azure-tools/typespec-azure-core": "workspace:~",
"@azure-tools/typespec-azure-resource-manager": "workspace:~",
"@azure-tools/typespec-client-generator-core": "workspace:~",
"@typespec/compiler": "workspace:~",
"@typespec/http": "workspace:~",
Expand All @@ -64,6 +65,7 @@
},
"devDependencies": {
"@azure-tools/typespec-azure-core": "workspace:~",
"@azure-tools/typespec-azure-resource-manager": "workspace:~",
"@azure-tools/typespec-client-generator-core": "workspace:~",
"@types/node": "~18.11.19",
"@typespec/compiler": "workspace:~",
Expand Down
48 changes: 3 additions & 45 deletions packages/typespec-autorest/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DecoratorContext, Model, ModelProperty, Program, Service, Type } from "@typespec/compiler";
import { DecoratorContext, Model, ModelProperty, Program, Type } from "@typespec/compiler";
import { createStateSymbol, reportDiagnostic } from "./lib.js";

export const namespace = "Autorest";
Expand Down Expand Up @@ -47,24 +47,6 @@ export function getExamples(program: Program, entity: Type): Example[] | undefin
return program.stateMap(exampleKey).get(entity);
}

/**
* Parameters that may be passed to a RefProducer function. Specific RefProducer
* functions may define additional parameters.
*/
export interface RefProducerParams {
service?: Service;
version?: string;
}

/**
* A function that can produce a ref path at the time it is requested.
*/
export type RefProducer = (
program: Program,
entity: Model | ModelProperty,
params: RefProducerParams
) => string | undefined;

const refTargetsKey = createStateSymbol("autorest.ref");
/**
* `@useRef` - is used to replace the TypeSpec model type in emitter output with a pre-existing named OpenAPI schema such as ARM common types.
Expand All @@ -77,31 +59,7 @@ export function $useRef(context: DecoratorContext, entity: Model | ModelProperty
context.program.stateMap(refTargetsKey).set(entity, jsonRef);
}

/**
* Configures a "ref producer" for the given entity. A ref producer is a
* function that returns a ref path for the given entity, possibly altered by
* the options provided to the function (like the service and version).
*
* @param {function} param producer - path or Uri to an OpenAPI schema.
*
*/
export function setRefProducer(
program: Program,
entity: Model | ModelProperty,
refProducer: RefProducer
) {
program.stateMap(refTargetsKey).set(entity, refProducer);
}

export function getRef(
program: Program,
entity: Type,
params?: RefProducerParams
): string | undefined {
export function getRef(program: Program, entity: Type): string | undefined {
const refOrProducer = program.stateMap(refTargetsKey).get(entity);
if (typeof refOrProducer === "function") {
return refOrProducer(program, entity, params ?? {});
} else {
return refOrProducer;
}
return refOrProducer;
}
42 changes: 30 additions & 12 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {
getUnionAsEnum,
isFixed,
} from "@azure-tools/typespec-azure-core";
import {
getArmCommonTypeOpenAPIRef,
isArmCommonType,
} from "@azure-tools/typespec-azure-resource-manager";
import { shouldFlattenProperty } from "@azure-tools/typespec-client-generator-core";
import {
ArrayModelType,
Expand Down Expand Up @@ -764,7 +768,7 @@ export async function getOpenAPIForService(
return header;
}

function resolveRef(ref: string) {
function expandRef(ref: string) {
const absoluteRef = interpolatePath(ref, {
"arm-types-dir": options.armTypesDir,
});
Expand All @@ -775,14 +779,33 @@ export async function getOpenAPIForService(
return getRelativePathFromDirectory(getDirectoryPath(context.outputFile), absoluteRef, false);
}

function getSchemaOrRef(type: Type, schemaContext: SchemaContext): any {
const refUrl = getRef(program, type, { version: context.version, service: context.service });
function resolveExternalRef(type: Type) {
const refUrl = getRef(program, type);
if (refUrl) {
return {
$ref: resolveRef(refUrl),
$ref: expandRef(refUrl),
};
}

if (isArmCommonType(type) && (type.kind === "Model" || type.kind === "ModelProperty")) {
const ref = getArmCommonTypeOpenAPIRef(program, type, {
version: context.version,
service: context.service,
});
if (ref) {
return {
$ref: expandRef(ref),
};
}
}
return undefined;
}
function getSchemaOrRef(type: Type, schemaContext: SchemaContext): any {
const ref = resolveExternalRef(type);
if (ref) {
return ref;
}

if (type.kind === "Scalar" && program.checker.isStdType(type)) {
return getSchemaForScalar(type);
}
Expand Down Expand Up @@ -870,14 +893,9 @@ export async function getOpenAPIForService(
}
}

const refUrl = getRef(program, property, {
version: context.version,
service: context.service,
});
if (refUrl) {
return {
$ref: resolveRef(refUrl),
};
const ref = resolveExternalRef(property);
if (ref) {
return ref;
}

const parameter = params.get(property);
Expand Down
Loading
Loading