Skip to content

Commit

Permalink
Generate schemas using autorest.bicep
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Apr 1, 2024
1 parent fb449dc commit 4f20acb
Show file tree
Hide file tree
Showing 11 changed files with 1,536 additions and 81 deletions.
115 changes: 115 additions & 0 deletions src/autorest.bicep/package-lock.json

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

3 changes: 3 additions & 0 deletions src/autorest.bicep/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"build": "tsc -p .",
"test": "jest",
"test:fix": "BASELINE_RECORD=true jest",
"start": "node ./dist/src/main.js",
"lint": "eslint src --ext ts",
"lint:fix": "eslint src --ext ts --fix"
Expand All @@ -15,6 +16,7 @@
"@types/lodash": "^4.17.0",
"autorest": "^3.7.1",
"bicep-types": "file:../../bicep-types/src/bicep-types",
"json-schema": "^0.4.0",
"lodash": "^4.17.21"
},
"devDependencies": {
Expand All @@ -28,6 +30,7 @@
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-jest": "^27.4.2",
"jest": "^27.5.1",
"json-diff": "^1.0.6",
"ts-jest": "^27.1.4",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
Expand Down
21 changes: 15 additions & 6 deletions src/autorest.bicep/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { AutoRestExtension, AutorestExtensionHost, startSession } from "@autorest/extension-base";
import { generateTypes } from "./type-generator";
import { generateSchema } from "./schema-generator";
import { CodeModel, codeModelSchema } from "@autorest/codemodel";
import { writeTypesJson, writeMarkdown } from "bicep-types";
import { getProviderDefinitions } from "./resources";
Expand All @@ -18,15 +19,23 @@ export async function processRequest(host: AutorestExtensionHost) {

for (const definition of getProviderDefinitions(session.model, host)) {
const { namespace, apiVersion } = definition;
const types = generateTypes(host, definition);

const outFolder = `${namespace}/${apiVersion}`.toLowerCase();

// write types.json
host.writeFile({ filename: `${outFolder}/types.json`, content: writeTypesJson(types) });

// writer types.md
host.writeFile({ filename: `${outFolder}/types.md`, content: writeMarkdown(types, `${namespace} @ ${apiVersion}`) });
if (!session.configuration["arm-schema"]) {
const types = generateTypes(host, definition);

// write types.json
host.writeFile({ filename: `${outFolder}/types.json`, content: writeTypesJson(types) });

// writer types.md
host.writeFile({ filename: `${outFolder}/types.md`, content: writeMarkdown(types, `${namespace} @ ${apiVersion}`) });
} else {
const schema = generateSchema(host, definition);

// write schema.json
host.writeFile({ filename: `${outFolder}/schema.json`, content: JSON.stringify(schema, null, 2) });
}
}

session.info(`autorest.bicep took ${Date.now() - start}ms`);
Expand Down
9 changes: 7 additions & 2 deletions src/autorest.bicep/src/resources.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { ChoiceSchema, CodeModel, ComplexSchema, HttpMethod, HttpParameter, HttpRequest, HttpResponse, ImplementationLocation, isObjectSchema, ObjectSchema, Operation, Parameter, ParameterLocation, Request, Response, Schema, SchemaResponse, SealedChoiceSchema, Metadata } from "@autorest/codemodel";
import { ChoiceSchema, CodeModel, ComplexSchema, HttpMethod, HttpParameter, HttpRequest, HttpResponse, ImplementationLocation, isObjectSchema, ObjectSchema, Operation, Parameter, ParameterLocation, Request, Response, Schema, SchemaResponse, SealedChoiceSchema, Metadata, ConstantSchema } from "@autorest/codemodel";
import { Channel, AutorestExtensionHost } from "@autorest/extension-base";
import { keys, Dictionary, values, groupBy, uniqBy, chain, flatten } from 'lodash';
import { success, failure, Result } from './utils';
Expand Down Expand Up @@ -115,6 +115,7 @@ export function getSerializedName(metadata: Metadata) {
interface ParameterizedName {
type: 'parameterized';
schema: Schema;
description?: string;
}

interface ConstantName {
Expand Down Expand Up @@ -146,7 +147,11 @@ export function getNameSchema(request: HttpRequest, parameters: Parameter[]): Re
return failure(`Unable to locate parameter with name '${resNameParam}'`);
}

return success({type: 'parameterized', schema: param.schema});
if (param.schema instanceof ConstantSchema) {
return success({ type: 'constant', value: param.schema.value.value.toString() });
}

return success({type: 'parameterized', schema: param.schema, description: param.language.default.description });
}

if (!/^[a-zA-Z0-9]*$/.test(resNameParam)) {
Expand Down
Loading

0 comments on commit 4f20acb

Please sign in to comment.