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

Handle circular dependency in additionalProperties #3819

Merged
merged 4 commits into from
Feb 1, 2021
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,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