Skip to content

Commit

Permalink
Handle circular dependency in additionalProperties (#3819)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Feb 1, 2021
1 parent 8e30959 commit e765f8b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@autorest/modelerfour",
"comment": "Fix the use of circular dependencies in additionalProperties [PR #3819](https://github.com/Azure/autorest/pull/3819)",
"type": "patch"
}
],
"packageName": "@autorest/modelerfour",
"email": "tiguerin@microsoft.com"
}
25 changes: 16 additions & 9 deletions packages/extensions/modelerfour/src/modeler/modelerfour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,14 @@ export class ModelerFour {
throw new Error("Method not implemented.");
}
processDictionarySchema(name: string, schema: OpenAPI.Schema): DictionarySchema {
const dictSchema = new DictionarySchema<any>(
this.interpret.getName(name, schema),
this.interpret.getDescription("", schema),
null,
);
// cache this now before we accidentally recurse on this type.
this.schemaCache.set(schema, dictSchema);

let elementSchema: Schema;
let elementNullable: boolean | undefined;
if (schema.additionalProperties === true) {
Expand All @@ -727,16 +735,14 @@ export class ModelerFour {
}
}

return this.codeModel.schemas.add(
new DictionarySchema(
this.interpret.getName(name, schema),
this.interpret.getDescription(`Dictionary of <${elementSchema.language.default.name}>`, schema),
elementSchema,
{
nullableItems: elementNullable,
},
),
dictSchema.language.default.description = this.interpret.getDescription(
`Dictionary of <${elementSchema.language.default.name}>`,
schema,
);
dictSchema.elementType = elementSchema;
dictSchema.nullableItems = elementNullable;

return this.codeModel.schemas.add(dictSchema);
}

isSchemaPolymorphic(schema: OpenAPI.Schema | undefined): boolean {
Expand Down Expand Up @@ -779,6 +785,7 @@ export class ModelerFour {
for (const { key: propertyName, value: propertyDeclaration } of items(schema.properties)) {
const property = this.resolve(propertyDeclaration);
this.use(<OpenAPI.Refable<OpenAPI.Schema>>propertyDeclaration, (pSchemaName, pSchema) => {

const pType = this.processSchema(pSchemaName || `type·for·${propertyName}`, pSchema);
const prop = objectSchema.addProperty(
new Property(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { addOperation, addSchema, createTestSpec } from "../utils";
import { runModeler } from "./modelerfour-utils";

describe("Modelerfour.Schemas", () => {
describe("additionalProperties", () => {
it("support reference to itself(circular dependency", async () => {
const spec = createTestSpec();

addSchema(spec, "SelfRefSchema", {
additionalProperties: {
$ref: "#/components/schemas/SelfRefSchema",
},
});

const codeModel = await runModeler(spec);

const schema = codeModel.schemas.dictionaries?.[0];
expect(schema).not.toBeNull();
expect(schema?.language.default.name).toEqual("SelfRefSchema");
expect(schema?.elementType).toEqual(schema);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
Expand Down

0 comments on commit e765f8b

Please sign in to comment.