Fix: Unreferenced discriminated union option being removed (#4337)

This commit is contained in:
Timothee Guerin 2021-10-11 15:21:18 -07:00 коммит произвёл GitHub
Родитель 5c8ab72f07
Коммит 5bb7288f84
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 189 добавлений и 18 удалений

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

@ -44,7 +44,7 @@
"typings": "./dist/exports.d.ts",
"devDependencies": {
"@autorest/configuration": "~1.7.2",
"@autorest/core": "~3.6.5",
"@autorest/core": "~3.6.6",
"@autorest/common": "~1.3.0",
"@azure-tools/async-io": "~3.0.0",
"@azure-tools/extension": "~3.3.1",

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

@ -1,6 +1,18 @@
{
"name": "@autorest/core",
"entries": [
{
"version": "3.6.6",
"tag": "@autorest/core_v3.6.6",
"date": "Mon, 11 Oct 2021 21:01:13 GMT",
"comments": {
"patch": [
{
"comment": "**Fix** Unreferenced discriminated union option being removed"
}
]
}
},
{
"version": "3.6.5",
"tag": "@autorest/core_v3.6.5",

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

@ -1,6 +1,13 @@
# Change Log - @autorest/core
This log was last generated on Wed, 06 Oct 2021 17:36:17 GMT and should not be manually modified.
This log was last generated on Mon, 11 Oct 2021 21:01:13 GMT and should not be manually modified.
## 3.6.6
Mon, 11 Oct 2021 21:01:13 GMT
### Patches
- **Fix** Unreferenced discriminated union option being removed
## 3.6.5
Wed, 06 Oct 2021 17:36:17 GMT

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

@ -1,6 +1,6 @@
{
"name": "@autorest/core",
"version": "3.6.5",
"version": "3.6.6",
"description": "AutoRest core module",
"engines": {
"node": ">=12.0.0"

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

@ -3,6 +3,7 @@
* Licensed under the MIT License.See License.txt in the project root for license information.
* --------------------------------------------------------------------------------------------*/
import { arrayify } from "@autorest/common";
import {
AnyObject,
DataHandle,
@ -218,20 +219,27 @@ class UnsuedComponentFinder {
private checkRef(
containerType: ComponentType,
currentComponentUid: string,
component: any,
component: oai3.Schema,
prop: "allOf" | "anyOf" | "oneOf" | "not",
) {
if (component[prop]?.$ref) {
const refParts = component[prop].$ref.split("/");
const componentRefUid = refParts.pop();
const refType = refParts.pop() as keyof ComponentTracker;
if (
this.componentsToKeep[refType].has(componentRefUid) &&
!this.componentsToKeep[containerType].has(currentComponentUid)
) {
this.componentsToKeep[containerType].add(currentComponentUid);
this.crawlObject(component);
return true;
const items = component[prop];
if (items === undefined) {
return;
}
for (const item of arrayify(items)) {
if (item.$ref) {
const refParts = item.$ref.split("/");
const componentRefUid = refParts.pop();
const refType = refParts.pop() as keyof ComponentTracker;
if (
this.componentsToKeep[refType].has(componentRefUid) &&
!this.componentsToKeep[containerType].has(currentComponentUid)
) {
this.componentsToKeep[containerType].add(currentComponentUid);
this.crawlObject(component);
return true;
}
}
}
return false;

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

@ -1,7 +1,7 @@
import { ComponentsCleaner } from "../../../src/lib/plugins/components-cleaner";
import assert from "assert";
import fs from "fs";
import { DataStore, MemoryFileSystem } from "@azure-tools/datastore";
import assert from "assert";
import { ComponentsCleaner } from "../../../src/lib/plugins/components-cleaner";
const readData = async (file: string) => {
const map = new Map<string, string>();
@ -55,4 +55,8 @@ describe("ComponentCleaner", () => {
const { input, output } = await runComponentCleaner("components-extensions");
expect(output).toEqual(input);
});
it("doesn't remove polymorhique types", async () => {
await expectScenarioToMatchSnapshot("polymorphism");
});
});

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

@ -0,0 +1,82 @@
{
"openapi": "3.0.0",
"info": {
"description": "",
"version": "1.0.0",
"title": ""
},
"paths": {},
"components": {
"schemas": {
"MainFileObj": {
"x-ms-metadata": {
"apiVersions": [
"1.0.0"
],
"x-ms-secondary-file": false
},
"type": "object",
"properties": {
"pet": {
"$ref": "#/components/schemas/Pet"
}
}
},
"Pet": {
"x-ms-metadata": {
"apiVersions": [
"1.0.0"
],
"x-ms-secondary-file": true
},
"type": "object",
"discriminator": "type",
"properties": {
"type": {
"type": "string"
}
}
},
"Cat": {
"x-ms-metadata": {
"apiVersions": [
"1.0.0"
],
"x-ms-secondary-file": true
},
"type": "object",
"x-ms-discriminator-value": "Cat",
"allOf": [
{
"$ref": "#/components/schemas/Pet"
}
],
"properties": {
"type": {
"meow": "string"
}
}
},
"Dog": {
"x-ms-metadata": {
"apiVersions": [
"1.0.0"
],
"x-ms-secondary-file": true
},
"type": "object",
"x-ms-discriminator-value": "Dog",
"allOf": [
{
"$ref": "#/components/schemas/Pet"
}
],
"properties": {
"type": {
"bark": "string"
}
}
}
}
}
}

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

@ -0,0 +1,58 @@
{
"openapi": "3.0.0",
"info": {
"description": "",
"version": "1.0.0",
"title": ""
},
"paths": {},
"components": {
"schemas": {
"MainFileObj": {
"x-ms-metadata": {
"apiVersions": ["1.0.0"],
"x-ms-secondary-file": false
},
"type": "object",
"properties": {
"pet": { "$ref": "#/components/schemas/Pet" }
}
},
"Pet": {
"x-ms-metadata": {
"apiVersions": ["1.0.0"],
"x-ms-secondary-file": true
},
"type": "object",
"discriminator": "type",
"properties": {
"type": { "type": "string" }
}
},
"Cat": {
"x-ms-metadata": {
"apiVersions": ["1.0.0"],
"x-ms-secondary-file": true
},
"type": "object",
"x-ms-discriminator-value": "Cat",
"allOf": [{ "$ref": "#/components/schemas/Pet" }],
"properties": {
"type": { "meow": "string" }
}
},
"Dog": {
"x-ms-metadata": {
"apiVersions": ["1.0.0"],
"x-ms-secondary-file": true
},
"type": "object",
"x-ms-discriminator-value": "Dog",
"allOf": [{ "$ref": "#/components/schemas/Pet" }],
"properties": {
"type": { "bark": "string" }
}
}
}
}
}

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

@ -30,7 +30,7 @@
},
"homepage": "https://github.com/Azure/autorest#readme",
"dependencies": {
"@autorest/core": "~3.6.5",
"@autorest/core": "~3.6.6",
"autorest": "~3.4.1",
"source-map-support": "^0.5.19"
},