This commit is contained in:
Timothee Guerin 2022-05-11 12:46:37 -07:00
Родитель efb9571acf c135831e02
Коммит a5b47c3c21
6 изменённых файлов: 112 добавлений и 6 удалений

Просмотреть файл

@ -1,6 +1,18 @@
{
"name": "@autorest/modelerfour",
"entries": [
{
"version": "4.23.4",
"tag": "@autorest/modelerfour_v4.23.4",
"date": "Mon, 09 May 2022 15:29:40 GMT",
"comments": {
"patch": [
{
"comment": "Fix `x-ms-client-flatten` defined at the root of the model would always flatten that model when referemced in properties"
}
]
}
},
{
"version": "4.23.3",
"tag": "@autorest/modelerfour_v4.23.3",

Просмотреть файл

@ -1,6 +1,13 @@
# Change Log - @autorest/modelerfour
This log was last generated on Tue, 03 May 2022 20:20:34 GMT and should not be manually modified.
This log was last generated on Mon, 09 May 2022 15:29:40 GMT and should not be manually modified.
## 4.23.4
Mon, 09 May 2022 15:29:40 GMT
### Patches
- Fix `x-ms-client-flatten` defined at the root of the model would always flatten that model when referemced in properties
## 4.23.3
Tue, 03 May 2022 20:20:34 GMT

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "@autorest/modelerfour",
"version": "4.23.3",
"version": "4.23.4",
"description": "AutoRest Modeler Version Four (component)",
"directories": {
"doc": "docs"

Просмотреть файл

@ -867,6 +867,7 @@ export class ModelerFour {
this.schemaCache.set(schema, objectSchema);
for (const [propertyName, propertyDeclaration] of Object.entries(schema.properties ?? {})) {
this.use(<OpenAPI.Refable<OpenAPI.Schema>>propertyDeclaration, (pSchemaName, pSchema) => {
const property = this.resolve(propertyDeclaration);
const pType = this.processSchema(pSchemaName || `type·for·${propertyName}`, pSchema);
const prop = objectSchema.addProperty(
new Property(
@ -880,8 +881,8 @@ export class ModelerFour {
required: schema.required ? schema.required.indexOf(propertyName) > -1 : undefined,
serializedName: propertyName,
isDiscriminator: discriminatorProperty === propertyName ? true : undefined,
extensions: this.interpret.getExtensionProperties(pSchema, propertyDeclaration),
clientDefaultValue: this.interpret.getClientDefault(pSchema, propertyDeclaration),
extensions: this.interpret.getExtensionProperties(property, propertyDeclaration),
clientDefaultValue: this.interpret.getClientDefault(property, propertyDeclaration),
},
),
);

Просмотреть файл

@ -2,6 +2,7 @@ import assert from "assert";
import { CodeModel, Operation } from "@autorest/codemodel";
import oai3, { Model } from "@azure-tools/openapi";
import { ModelerFourOptions } from "modeler/modelerfour-options";
import { Flattener } from "../../src/flattener/flattener";
import { ModelerFour } from "../../src/modeler/modelerfour";
import { addOperation, createTestSessionFromModel, createTestSpec } from "../utils";
@ -39,6 +40,21 @@ export async function runModeler(spec: any, config: { modelerfour: ModelerFourOp
return result;
}
export async function runFlattener(
codemodel: CodeModel,
config: { modelerfour: ModelerFourOptions } = cfg,
): Promise<CodeModel> {
const { session, errors } = await createTestSessionFromModel<CodeModel>(config, codemodel);
session.model = codemodel;
const flattener = await new Flattener(session).init();
expect(errors).toHaveLength(0);
const result = flattener.process();
expect(errors).toHaveLength(0);
return result;
}
export async function runModelerWithOperation(
method: string,
path: string,

Просмотреть файл

@ -1,8 +1,9 @@
import assert from "assert";
import { ChoiceSchema, ConstantSchema, SealedChoiceSchema } from "@autorest/codemodel";
import { JsonType } from "@azure-tools/openapi";
import { JsonType, OpenAPI3Document } from "@azure-tools/openapi";
import { ModelerFourOptions } from "modeler/modelerfour-options";
import { addSchema, assertSchema, createTestSpec, findByName } from "../utils";
import { runModeler } from "./modelerfour-utils";
import { runFlattener, runModeler } from "./modelerfour-utils";
describe("Modelerfour.Schemas", () => {
describe("additionalProperties", () => {
@ -404,4 +405,73 @@ describe("Modelerfour.Schemas", () => {
expect(parent?.properties?.find((x) => x.serializedName === "child")?.schema).toEqual(child);
});
});
describe("x-ms-client-flatten", () => {
async function runModelerWithFlattener(spec: OpenAPI3Document, config: { modelerfour: ModelerFourOptions }) {
const codeModel = await runModeler(spec, config);
return runFlattener(codeModel, config);
}
it("doesn't flatten if x-ms-client-flatten is specified on the property", async () => {
const spec = createTestSpec();
addSchema(spec, "Foo", {
type: "object",
properties: {
nestedBar: { $ref: "#/components/schemas/Bar", "x-ms-client-flatten": true },
},
});
addSchema(spec, "Bar", {
type: "object",
properties: {
bar: {
type: "string",
},
},
});
const codeModel = await runModelerWithFlattener(spec, { modelerfour: { "flatten-models": true } });
const foo = findByName("Foo", codeModel.schemas.objects);
expect(foo).toBeDefined();
const nestedBarProp = findByName("nestedBar", foo?.properties);
expect(nestedBarProp).not.toBeDefined();
const flattendBarProp = findByName("bar", foo?.properties);
expect(flattendBarProp).toBeDefined();
});
it("doesn't flatten if x-ms-client-flatten is specified on the target model", async () => {
const spec = createTestSpec();
addSchema(spec, "Foo", {
type: "object",
properties: {
nestedBar: { $ref: "#/components/schemas/Bar" },
},
});
addSchema(spec, "Bar", {
"x-ms-client-flatten": true,
type: "object",
properties: {
bar: {
type: "string",
},
},
});
const codeModel = await runModelerWithFlattener(spec, { modelerfour: { "flatten-models": true } });
const foo = findByName("Foo", codeModel.schemas.objects);
expect(foo).toBeDefined();
const bar = findByName("Bar", codeModel.schemas.objects);
expect(bar).toBeDefined();
const nestedBarProp = findByName("nestedBar", foo?.properties);
expect(nestedBarProp).toBeDefined();
expect(nestedBarProp?.schema).toEqual(bar);
});
});
});