Skip to content

frontend: Add tests for @apidevtools/swagger-parser #2822

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

Merged
Merged
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
132 changes: 132 additions & 0 deletions frontend/src/lib/swagger-parser.k8s.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import Swagger from '@apidevtools/swagger-parser';
import { OpenAPIV2 } from 'openapi-types';

describe('@apidevtools/swagger-parser', () => {
it('should resolve $ref references', async () => {
const doc: OpenAPIV2.Document = {
swagger: '2.0',
info: { title: 'Test API', version: '1.0.0' },
paths: {},
definitions: {
'io.k8s.api.apps.v1.Deployment': {
type: 'object',
properties: {
metadata: { $ref: '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta' },
},
},
'io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta': {
type: 'object',
properties: {
name: { type: 'string' },
namespace: { type: 'string' },
},
},
},
};

const result = (await Swagger.dereference(doc)) as OpenAPIV2.Document;

expect(result.definitions?.['io.k8s.api.apps.v1.Deployment'].properties?.metadata).toEqual({
type: 'object',
properties: {
name: { type: 'string' },
namespace: { type: 'string' },
},
});
});

it('should throw an error for invalid $ref references', async () => {
const doc: OpenAPIV2.Document = {
swagger: '2.0',
info: { title: 'Test API', version: '1.0.0' },
paths: {},
definitions: {
'io.k8s.api.core.v1.Pod': {
type: 'object',
properties: {
metadata: { $ref: '#/definitions/NonExistentRef' },
},
},
},
};

await expect(Swagger.dereference(doc)).rejects.toThrow(/Token "NonExistentRef"/);
});

it('should handle deeply nested $ref references', async () => {
const doc: OpenAPIV2.Document = {
swagger: '2.0',
info: { title: 'Test API', version: '1.0.0' },
paths: {},
definitions: {
'io.k8s.api.core.v1.Pod': {
type: 'object',
properties: {
spec: { $ref: '#/definitions/io.k8s.api.core.v1.PodSpec' },
},
},
'io.k8s.api.core.v1.PodSpec': {
type: 'object',
properties: {
containers: {
type: 'array',
items: { $ref: '#/definitions/io.k8s.api.core.v1.Container' },
},
},
},
'io.k8s.api.core.v1.Container': {
type: 'object',
properties: {
image: { type: 'string' },
},
},
},
};

const result = (await Swagger.dereference(doc)) as OpenAPIV2.Document;

expect(result.definitions?.['io.k8s.api.core.v1.Pod'].properties?.spec).toEqual({
type: 'object',
properties: {
containers: {
type: 'array',
items: { type: 'object', properties: { image: { type: 'string' } } },
},
},
});
});

it('should handle circular references', async () => {
const doc: OpenAPIV2.Document = {
swagger: '2.0',
info: { title: 'Test API', version: '1.0.0' },
paths: {},
definitions: {
'io.k8s.api.core.v1.Pod': {
type: 'object',
properties: {
metadata: {
$ref: '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta',
},
},
},
'io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta': {
type: 'object',
properties: {
ownerReferences: {
$ref: '#/definitions/io.k8s.api.core.v1.Pod',
},
},
},
},
};

const result = (await Swagger.dereference(doc)) as OpenAPIV2.Document;

expect(result.definitions?.['io.k8s.api.core.v1.Pod'].properties?.metadata).toBeDefined();
expect(
result.definitions?.['io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta'].properties
?.ownerReferences
).toBeDefined();
});
});
Loading