Handle using properties called $ref (#3922)

This commit is contained in:
Timothee Guerin 2021-02-23 17:51:56 -08:00 коммит произвёл GitHub
Родитель be96b27b6a
Коммит cf5dc83fe2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 48 добавлений и 8 удалений

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

@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@autorest/core",
"comment": "**Fix** issue when using properties with `$ref` as name where it would try to resolve a reference.",
"type": "patch"
}
],
"packageName": "@autorest/core",
"email": "tiguerin@microsoft.com"
}

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

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable no-console */
import "source-map-support/register";
import { omit } from "lodash";
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js

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

@ -28,7 +28,7 @@ import { PipelinePlugin } from "../common";
*
*/
type componentType =
type ComponentType =
| "schemas"
| "responses"
| "parameters"
@ -165,10 +165,10 @@ export class ComponentsCleaner extends Transformer<any, oai.Model> {
if (key === "x-ms-examples") {
continue;
}
if (key === "$ref") {
if (key === "$ref" && typeof value === "string") {
const refParts = value.split("/");
const componentUid = refParts.pop();
const t: componentType = refParts.pop();
const componentUid = refParts.pop() as string;
const t: ComponentType = refParts.pop() as ComponentType;
if (!this.visitedComponents[t].has(componentUid)) {
this.visitedComponents[t].add(componentUid);
this.componentsToKeep[t].add(componentUid);

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

@ -206,7 +206,7 @@ export class MultiAPIMerger extends Transformer<any, oai.Model> {
for (const { value } of visit(node)) {
if (value && typeof value === "object") {
const ref = value.$ref;
if (ref && ref.startsWith("#")) {
if (ref && typeof ref === "string" && ref.startsWith("#")) {
const fullRef = `${(<DataHandle>this.currentInput).originalFullPath}${ref}`;
// change local refs to full ref
value.$ref = fullRef;
@ -275,7 +275,7 @@ export class MultiAPIMerger extends Transformer<any, oai.Model> {
}
if (value && typeof value === "object") {
const ref = value.$ref;
if (ref) {
if (ref && typeof ref === "string") {
// see if this object has a $ref
const newRef = this.refs[ref];
if (newRef) {

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

@ -95,6 +95,7 @@ class RefProcessor extends Transformer<any, any> {
constructor(originalFile: DataHandle, private inputScope: DataSource) {
super(originalFile);
this.originalFileLocation = ResolveUri(originalFile.originalDirectory, originalFile.identity[0]);
}
@ -129,7 +130,8 @@ class RefProcessor extends Transformer<any, any> {
this.promises.push(this.processXMSExamples(targetParent, value));
continue;
}
if (key === "$ref") {
// If the key is $ref and the value is a string then it should be a json reference. Otherwise it might be a property called $ref if it is another type.
if (key === "$ref" && typeof value === "string") {
const refFileName = value.indexOf("#") === -1 ? value : value.split("#")[0];
const refPointer = value.indexOf("#") === -1 ? undefined : value.split("#")[1];
const newRefFileName = ResolveUri(this.originalFileLocation, refFileName);

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

@ -54,4 +54,9 @@ describe("ComponentCleaner", () => {
it("secondary-file components not referenced by something in a primary-file.", async () => {
await expectScenarioToMatchSnapshot("some-unused-secondary-components");
});
it("ignores schema properties called $ref", async () => {
const { input, output } = await runComponentCleaner("schema-with-$ref-property");
expect(output).toEqual(input);
});
});

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

@ -0,0 +1,22 @@
{
"openapi": "3.0.0",
"info": {
"description": "This is a simple openapi spec where a component has a property called $ref. This should be handled gracefully.",
"version": "1.0.0",
"title": "Swagger Petstore"
},
"paths": {},
"components": {
"schemas": {
"MyComponent": {
"x-ms-metadata": {
"apiVersions": ["1.0.0"],
"x-ms-secondary-file": false
},
"properties": {
"$ref": { "type": "string" }
}
}
}
}
}