persona-community-5/.pnpm-store/v3/files/06/4d5ec61bc2f6a61c70589f73d1bde6b353a77b834a26740479af420c26d5403142f6711f4aa1c403c45133dc05600cd11a0101a98acb5b5fb5a979143159b4
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

314 lines
7.4 KiB
Plaintext

import { outdent } from 'outdent';
import { parseYamlToDocument, makeConfig } from '../../../../__tests__/utils';
import { bundleDocument } from '../../../bundle';
import { BaseResolver } from '../../../resolve';
describe('oas3 remove-unused-components', () => {
it('should remove unused components', async () => {
const document = parseYamlToDocument(
outdent`
openapi: "3.0.0"
paths:
/pets:
get:
summary: List all pets
operationId: listPets
parameters:
- $ref: '#/components/parameters/used'
components:
parameters:
used:
name: used
unused:
name: unused
responses:
unused: {}
examples:
unused: {}
requestBodies:
unused: {}
headers:
unused: {}
schemas:
Unused:
type: integer
enum:
- 1
- 2
`,
'foobar.yaml'
);
const results = await bundleDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ rules: {} }),
removeUnusedComponents: true,
});
expect(results.bundle.parsed).toEqual({
openapi: '3.0.0',
paths: {
'/pets': {
get: {
summary: 'List all pets',
operationId: 'listPets',
parameters: [
{
$ref: '#/components/parameters/used',
},
],
},
},
},
components: {
parameters: {
used: {
name: 'used',
},
},
},
});
});
it('should not remove components used child reference', async () => {
const document = parseYamlToDocument(
outdent`
openapi: "3.0.0"
paths:
/pets:
get:
summary: List all pets
operationId: listPets
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Used'
components:
parameters:
unused:
name: unused
responses:
unused: {}
examples:
unused: {}
requestBodies:
unused: {}
headers:
unused: {}
schemas:
InnerUsed:
type: object
properties:
link:
type: string
Used:
type: object
properties:
link:
$ref: '#/components/schemas/InnerUsed/properties/link'
Unused:
type: integer
enum:
- 1
- 2
`,
'foobar.yaml'
);
const results = await bundleDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ rules: {} }),
removeUnusedComponents: true,
});
expect(results.bundle.parsed).toEqual({
openapi: '3.0.0',
paths: {
'/pets': {
get: {
summary: 'List all pets',
operationId: 'listPets',
responses: {
'200': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Used',
},
},
},
},
},
},
},
},
components: {
schemas: {
InnerUsed: {
type: 'object',
properties: {
link: {
type: 'string',
},
},
},
Used: {
type: 'object',
properties: {
link: {
$ref: '#/components/schemas/InnerUsed/properties/link',
},
},
},
},
},
});
});
it('should remove transitively unused components', async () => {
const document = parseYamlToDocument(
outdent`
openapi: "3.0.0"
paths:
/pets:
get:
summary: List all pets
operationId: listPets
parameters:
- $ref: '#/components/parameters/used'
components:
parameters:
used:
name: used
unused:
name: unused
schemas:
Unused:
type: integer
enum:
- 1
- 2
Transitive:
type: object
properties:
link:
$ref: '#/components/schemas/Unused'
`,
'foobar.yaml'
);
const results = await bundleDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ rules: {} }),
removeUnusedComponents: true,
});
expect(results.bundle.parsed).toEqual({
openapi: '3.0.0',
paths: {
'/pets': {
get: {
summary: 'List all pets',
operationId: 'listPets',
parameters: [
{
$ref: '#/components/parameters/used',
},
],
},
},
},
components: {
parameters: {
used: {
name: 'used',
},
},
},
});
});
it('should remove transitively unused components with colliding paths', async () => {
const document = parseYamlToDocument(
outdent`
openapi: "3.0.0"
paths:
/pets:
get:
responses:
200:
content:
application/json:
schema:
$ref: "#/components/schemas/Transitive2"
components:
schemas:
Unused: # <-- this will be removed correctly
type: integer
Transitive: # <-- this will be removed correctly
type: object
properties:
link:
$ref: '#/components/schemas/Unused'
Used:
type: integer
Transitive2:
type: object
properties:
link:
$ref: '#/components/schemas/Used'
`,
'foobar.yaml'
);
const results = await bundleDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ rules: {} }),
removeUnusedComponents: true,
});
expect(results.bundle.parsed).toEqual({
openapi: '3.0.0',
paths: {
'/pets': {
get: {
responses: {
200: {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Transitive2',
},
},
},
},
},
},
},
},
components: {
schemas: {
Transitive2: {
type: 'object',
properties: {
link: {
$ref: '#/components/schemas/Used',
},
},
},
Used: {
type: 'integer',
},
},
},
});
});
});