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

Resource model envelope properties #5065

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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,10 @@
{
"changes": [
{
"packageName": "@autorest/openapi-to-typespec",
"comment": "Add resourcemodel envelope properties",
"type": "patch"
}
],
"packageName": "@autorest/openapi-to-typespec"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Session } from "@autorest/extension-base";

let _session: Session<CodeModel>;
let _armCommonTypeVersion: string;
let _userSetArmCommonTypeVersion: string;

export function setSession(session: Session<CodeModel>): void {
_session = session;
Expand All @@ -13,9 +14,20 @@ export function getSession(): Session<CodeModel> {
}

export function setArmCommonTypeVersion(version: string): void {
_armCommonTypeVersion = version;
_userSetArmCommonTypeVersion = version;
}

export function getArmCommonTypeVersion(): string {
if (!_armCommonTypeVersion) {
if (["v3", "v4", "v5", "v6"].includes(_userSetArmCommonTypeVersion)) {
_armCommonTypeVersion = _userSetArmCommonTypeVersion;
} else {
_armCommonTypeVersion = "v3"; // We hardcode the common type version to v3 if it's not set or the value is not valid, otherwise no model can extend a resource model.
}
}
return _armCommonTypeVersion;
}

export function getUserSetArmCommonTypeVersion(): string {
return _userSetArmCommonTypeVersion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import { Case } from "change-case-all";
import _ from "lodash";
import pluralize from "pluralize";
import {
TypespecOperation,
TspArmResource,
TypespecTemplateModel,
TypespecVoidType,
TspLroHeaders,
TypespecParameter,
TypespecDataType,
TspArmResourceOperationGroup,
TspArmOperationType,
} from "../interfaces";
import { getOptions } from "../options";
import { getTSPOperationGroupName } from "../transforms/transform-arm-resources";
import { generateAugmentedDecorators, generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { getLogger } from "../utils/logger";
import { generateLroHeaders } from "../utils/lro";
import { getModelPropertiesDeclarations } from "../utils/model-generation";
import {
generateTemplateModel,
getModelPropertyDeclarations,
getSpreadExpressionDecalaration,
} from "../utils/model-generation";
import { generateSuppressions } from "../utils/suppressions";
import { generateOperation, generateParameters } from "./generate-operations";
import { generateParameters } from "./generate-operations";
import { generateParameter } from "./generate-parameter";

const logger = () => getLogger("generate-arm-resource");
Expand Down Expand Up @@ -50,7 +51,7 @@ export function generateArmResource(resource: TspArmResource): string {
}

function generateArmResourceModel(resource: TspArmResource): string {
let definitions: string[] = [];
const definitions: string[] = [];

for (const fixme of resource.fixMe ?? []) {
definitions.push(fixme);
Expand All @@ -76,13 +77,9 @@ function generateArmResourceModel(resource: TspArmResource): string {
}> {`,
);

if (resource.keyExpression) {
definitions.push(`${resource.keyExpression};`);
}
definitions = [...definitions, ...getModelPropertiesDeclarations(resource.properties)];

for (const p of resource.optionalStandardProperties) {
definitions.push(`\n...${p}`);
for (const property of resource.properties) {
if (property.kind === "property") definitions.push(...getModelPropertyDeclarations(property));
else if (property.kind === "spread") definitions.push(getSpreadExpressionDecalaration(property));
}

definitions.push("}\n");
Expand Down Expand Up @@ -238,20 +235,6 @@ function generateArmResponse(responses: TypespecTemplateModel[] | TypespecVoidTy
return responses.map((r) => generateTemplateModel(r)).join(" | ");
}

export function generateTemplateModel(templateModel: TypespecTemplateModel): string {
return `${templateModel.name}${
templateModel.arguments
? `<${templateModel.arguments
.map((a) => (a.kind === "template" ? generateTemplateModel(a as TypespecTemplateModel) : a.name))
.join(",")}>`
: ""
}${
templateModel.additionalProperties
? ` & { ${templateModel.additionalProperties.map((p) => generateParameter(p)).join(";")} }`
: ""
}${templateModel.additionalTemplateModel ? templateModel.additionalTemplateModel : ""}`;
}

export function generateArmResourceExamples(resource: TspArmResource): Record<string, string> {
const formalOperationGroupName = pluralize(resource.name);
const examples: Record<string, string> = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function generateArmResourceClientDecorator(resource: TspArmResource): st
}

for (const property of resource.properties) {
if (property.kind !== "property") continue;
const decorators = generateAugmentedDecorators(`${targetName}.${property.name}`, property.clientDecorators);
decorators && definitions.push(decorators);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { TypespecEnum } from "../interfaces";
import { getOptions } from "../options";
import { generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import {
generateSuppressionForDocumentRequired,
generateSuppressionForNoEnum,
generateSuppressions,
} from "../utils/suppressions";
import { generateSuppressionForNoEnum, generateSuppressions, getSuppresssionWithCode } from "../utils/suppressions";

export function generateEnums(typespecEnum: TypespecEnum) {
const { isFullCompatible } = getOptions();
const definitions: string[] = [];
const doc = generateDocs(typespecEnum);
definitions.push(doc.length > 0 || !isFullCompatible ? doc : `${generateSuppressionForDocumentRequired()}\n`);
definitions.push(
doc.length > 0 || !isFullCompatible
? doc
: `${generateSuppressions([
getSuppresssionWithCode("@azure-tools/typespec-azure-core/documentation-required"),
])}\n`,
);

const isExtensible = typespecEnum.isExtensible && !["ApiVersion"].includes(typespecEnum.name);
if (!isExtensible && isFullCompatible) definitions.push(`${generateSuppressionForNoEnum()}\n`);
Expand All @@ -37,7 +39,10 @@ export function generateEnums(typespecEnum: TypespecEnum) {
const doc = generateDocs(m);
const kv = `"${m.name}": ${m.value}`;
if (doc.length > 0 || !isFullCompatible) return `${doc}${kv}`;
else return `${generateSuppressionForDocumentRequired()}\n${kv}`;
else
return `${generateSuppressions([
getSuppresssionWithCode("@azure-tools/typespec-azure-core/documentation-required"),
Copy link
Member

Choose a reason for hiding this comment

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

consider to declare a const string "@azure-tools/typespec-azure-core/documentation-required" to avoid the duplicates

])}\n${kv}`;
})
.join(", ")}
}\n\n`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import { getOptions } from "../options";
import { generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { getModelPropertiesDeclarations } from "../utils/model-generation";
import { generateSuppressionForDocumentRequired, generateSuppressions } from "../utils/suppressions";
import { generateSuppressions } from "../utils/suppressions";

export function generateObject(typespecObject: TypespecObject) {
const { isFullCompatible } = getOptions();
let definitions: string[] = [];

const fixme = getFixme(typespecObject);
fixme && definitions.push(fixme);

const doc = generateDocs(typespecObject);
if (doc === "" && isFullCompatible) definitions.push(generateSuppressionForDocumentRequired());
definitions.push(doc);

const decorators = generateDecorators(typespecObject.decorators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TypespecParameter } from "../interfaces";
import { getOptions } from "../options";
import { generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { generateSuppressionForDocumentRequired, generateSuppressions } from "../utils/suppressions";
import { generateSuppressions } from "../utils/suppressions";

const _ARM_PARAM_REPLACEMENTS: { [key: string]: string } = {
subscriptionId: "...SubscriptionIdParameter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { getOptions } from "../options";
import { generateDocs } from "../utils/docs";
import { getNamespaceStatement } from "../utils/namespace";

const VALID_VERSIONS = ["v3", "v4", "v5"];

export function generateServiceInformation(program: TypespecProgram) {
const { serviceInformation } = program;
const definitions: string[] = [];
Expand All @@ -24,18 +22,18 @@ export function generateServiceInformation(program: TypespecProgram) {
}

if (isArm && serviceInformation.armCommonTypeVersion) {
if (VALID_VERSIONS.includes(serviceInformation.armCommonTypeVersion)) {
definitions.push(
`@armCommonTypesVersion(Azure.ResourceManager.CommonTypes.Versions.${serviceInformation.armCommonTypeVersion})`,
);
} else {
definitions.push(
`// FIXME: Common type version ${serviceInformation.armCommonTypeVersion} is not supported for now.`,
);
if (serviceInformation.armCommonTypeVersion !== serviceInformation.userSetArmCommonTypeVersion) {
definitions.push(
`// @armCommonTypesVersion(Azure.ResourceManager.CommonTypes.Versions.${serviceInformation.armCommonTypeVersion})`,
`// FIXME: ${
serviceInformation.userSetArmCommonTypeVersion
? `Common type version ${serviceInformation.userSetArmCommonTypeVersion} is not supported for now.`
: "Common type version not set."
} Set to v3.`,
);
}
definitions.push(
`@armCommonTypesVersion(Azure.ResourceManager.CommonTypes.Versions.${serviceInformation.armCommonTypeVersion})`,
);
}

if (!isArm && serviceInformation.endpoint) {
Expand Down
15 changes: 11 additions & 4 deletions packages/extensions/openapi-to-typespec/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface WithSummary {
export interface WithDecorators {
decorators?: TypespecDecorator[];
clientDecorators?: TypespecDecorator[];
augmentDecorators?: TypespecDecorator[];
}

export interface TypespecOperationGroup extends WithDoc, WithSuppressDirectives {
Expand Down Expand Up @@ -101,6 +102,7 @@ export interface ServiceInformation extends WithDoc {
consumes?: string[];
authentication?: Auth[];
armCommonTypeVersion?: string;
userSetArmCommonTypeVersion?: string;
}

export interface EndpointParameter extends WithDoc {
Expand All @@ -122,6 +124,7 @@ export type TypespecModel = TypespecTemplateModel | TypespecDataType;
export interface TypespecTemplateModel extends TypespecDataType {
kind: "template";
arguments?: TypespecModel[];
namedArguments?: Record<string, string>; // TO-DO: value is string for now, should be refacted to some object type
additionalProperties?: TypespecParameter[]; // Currently for body purpose
additionalTemplateModel?: string; // Currently for LRO header purpose
}
Expand Down Expand Up @@ -182,6 +185,12 @@ export interface TypespecObjectProperty extends TypespecDataType, WithSuppressDi
defaultValue?: any;
}

// A spread statement is always spreading some model or template from library
export interface TypespecSpreadStatement extends WithSuppressDirectives, WithDecorators, WithDoc {
kind: "spread";
model: TypespecTemplateModel;
}

export interface TypespecDecorator extends WithFixMe, WithSuppressDirective {
name: string;
arguments?: (string | number | string[])[] | DecoratorArgument[];
Expand Down Expand Up @@ -321,17 +330,15 @@ export interface TspArmProviderActionOperation extends WithDoc, WithSummary, Wit
lroHeaders?: TspLroHeaders;
}

export interface TspArmResource extends TypespecObject {
export interface TspArmResource extends TypespecDataType, WithFixMe, WithDoc, WithDecorators {
resourceKind: ArmResourceKind;
keyExpression: string | undefined;
properties: (TypespecObjectProperty | TypespecSpreadStatement)[];
propertiesModelName: string;
propertiesPropertyRequired: boolean;
propertiesPropertyDescription: string;
propertiesPropertyClientDecorator: TypespecDecorator[];
resourceParent?: TspArmResource;
resourceOperationGroups: TspArmResourceOperationGroup[];
optionalStandardProperties: string[];
baseModelName?: string;
locationParent?: string;
}

Expand Down
Loading
Loading