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

feat(ns-openapi-2): add support for Swagger Object #3313

Merged
merged 1 commit into from
Oct 24, 2023
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
2 changes: 1 addition & 1 deletion packages/apidom-ns-openapi-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const swaggerElement = SwaggerElement.refract(apiDOM.result, {

Only fully implemented specification objects should be checked here.

- [ ] [Swagger Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-swagger-object)
- [x] [Swagger Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-swagger-object)
- [x] [Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-info-object)
- [x] [Contact Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-contact-object)
- [x] [License Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-license-object)
Expand Down
146 changes: 146 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/Swagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import {
ObjectElement,
ArrayElement,
StringElement,
Attributes,
Meta,
} from '@swagger-api/apidom-core';

import SwaggerVersionElement from './SwaggerVersion';
import InfoElement from './Info';
import PathsElement from './Paths';
import DefinitionsElement from './Definitions';
import ParametersDefinitionsElement from './ParametersDefinitions';
import ResponsesDefinitionsElement from './ResponsesDefinitions';
import SecurityDefinitionsElement from './SecurityDefinitions';
import ExternalDocumentationElement from './ExternalDocumentation';

class Swagger extends ObjectElement {
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'swagger';
this.classes.push('api');
}

get swagger(): SwaggerVersionElement | undefined {
return this.get('swagger');
}

set swagger(swagger: SwaggerVersionElement | undefined) {
this.set('swagger', swagger);
}

get info(): InfoElement | undefined {
return this.get('info');
}

set info(info: InfoElement | undefined) {
this.set('info', info);
}

get host(): StringElement | undefined {
return this.get('host');
}

set host(host: StringElement | undefined) {
this.set('host', host);
}

get basePath(): StringElement | undefined {
return this.get('basePath');
}

set basePath(basePath: StringElement | undefined) {
this.set('basePath', basePath);
}

get schemes(): ArrayElement | undefined {
return this.get('schemes');
}

set schemes(schemes: ArrayElement | undefined) {
this.set('schemes', schemes);
}

get consumes(): ArrayElement | undefined {
return this.get('consumes');
}

set consumes(consumes: ArrayElement | undefined) {
this.set('consumes', consumes);
}

get produces(): ArrayElement | undefined {
return this.get('produces');
}

set produces(produces: ArrayElement | undefined) {
this.set('produces', produces);
}

get paths(): PathsElement | undefined {
return this.get('paths');
}

set paths(paths: PathsElement | undefined) {
this.set('paths', paths);
}

get definitions(): DefinitionsElement | undefined {
return this.get('definitions');
}

set definitions(definitions: DefinitionsElement | undefined) {
this.set('definitions', definitions);
}

get parameters(): ParametersDefinitionsElement | undefined {
return this.get('parameters');
}

set parameters(parameters: ParametersDefinitionsElement | undefined) {
this.set('parameters', parameters);
}

get responses(): ResponsesDefinitionsElement | undefined {
return this.get('responses');
}

set responses(responses: ResponsesDefinitionsElement | undefined) {
this.set('responses', responses);
}

get securityDefinitions(): SecurityDefinitionsElement | undefined {
return this.get('securityDefinitions');
}

set securityDefinitions(securityDefinitions: SecurityDefinitionsElement | undefined) {
this.set('securityDefinitions', securityDefinitions);
}

get security(): ArrayElement | undefined {
return this.get('security');
}

set security(security: ArrayElement | undefined) {
this.set('security', security);
}

get tags(): ArrayElement | undefined {
return this.get('tags');
}

set tags(tags: ArrayElement | undefined) {
this.set('tags', tags);
}

get externalDocs(): ExternalDocumentationElement | undefined {
return this.get('externalDocs');
}

set externalDocs(externalDocs: ExternalDocumentationElement | undefined) {
this.set('externalDocs', externalDocs);
}
}

export default Swagger;
12 changes: 12 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/SwaggerVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { StringElement, Attributes, Meta } from '@swagger-api/apidom-core';

class SwaggerVersion extends StringElement {
constructor(content?: string, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'swaggerVersion';
this.classes.push('spec-version');
this.classes.push('version');
}
}

export default SwaggerVersion;
12 changes: 12 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/nces/SwaggerConsumes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ArrayElement, Attributes, Meta } from '@swagger-api/apidom-core';

class SwaggerConsumes extends ArrayElement {
static primaryClass = 'swagger-consumes';

constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.classes.push(SwaggerConsumes.primaryClass);
}
}

export default SwaggerConsumes;
12 changes: 12 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/nces/SwaggerProduces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ArrayElement, Attributes, Meta } from '@swagger-api/apidom-core';

class SwaggerProduces extends ArrayElement {
static primaryClass = 'swagger-produces';

constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.classes.push(SwaggerProduces.primaryClass);
}
}

export default SwaggerProduces;
12 changes: 12 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/nces/SwaggerSchemes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ArrayElement, Attributes, Meta } from '@swagger-api/apidom-core';

class SwaggerSchemes extends ArrayElement {
static primaryClass = 'swagger-schemes';

constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.classes.push(SwaggerSchemes.primaryClass);
}
}

export default SwaggerSchemes;
13 changes: 13 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/nces/SwaggerSecurity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ArrayElement, Attributes, Meta } from '@swagger-api/apidom-core';

class SwaggerSecurity extends ArrayElement {
static primaryClass = 'swagger-security';

constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.classes.push(SwaggerSecurity.primaryClass);
this.classes.push('security');
}
}

export default SwaggerSecurity;
12 changes: 12 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/nces/SwaggerTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ArrayElement, Attributes, Meta } from '@swagger-api/apidom-core';

class SwaggerTags extends ArrayElement {
static primaryClass = 'swagger-tags';

constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.classes.push(SwaggerTags.primaryClass);
}
}

export default SwaggerTags;
9 changes: 9 additions & 0 deletions packages/apidom-ns-openapi-2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export { default as refract, createRefractor } from './refractor';
export { default as specificationObj } from './refractor/specification';

export {
isSwaggerElement,
isSwaggerVersionElement,
isInfoElement,
isContactElement,
isLicenseElement,
Expand Down Expand Up @@ -55,6 +57,8 @@ export { keyMap, getNodeType } from './traversal/visitor';

// OpenAPI 2.0 elements
export {
SwaggerElement,
SwaggerVersionElement,
InfoElement,
ContactElement,
LicenseElement,
Expand Down Expand Up @@ -89,3 +93,8 @@ export { default as OperationSchemesElement } from './elements/nces/OperationSch
export { default as OperationSecurityElement } from './elements/nces/OperationSecurity';
export { default as OperationTagsElement } from './elements/nces/OperationTags';
export { default as PathItemParametersElement } from './elements/nces/PathItemParameters';
export { default as SwaggerSchemesElement } from './elements/nces/SwaggerSchemes';
export { default as SwaggerConsumesElement } from './elements/nces/SwaggerConsumes';
export { default as SwaggerProducesElement } from './elements/nces/SwaggerProduces';
export { default as SwaggerSecurityElement } from './elements/nces/SwaggerSecurity';
export { default as SwaggerTagsElement } from './elements/nces/SwaggerTags';
4 changes: 4 additions & 0 deletions packages/apidom-ns-openapi-2/src/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NamespacePluginOptions } from '@swagger-api/apidom-core';

import SwaggerElement from './elements/Swagger';
import SwaggerVersionElement from './elements/SwaggerVersion';
import InfoElement from './elements/Info';
import ContactElement from './elements/Contact';
import LicenseElement from './elements/License';
Expand Down Expand Up @@ -30,6 +32,8 @@ const openApi2 = {
namespace: (options: NamespacePluginOptions) => {
const { base } = options;

base.register('swagger', SwaggerElement);
base.register('swaggerVersion', SwaggerVersionElement);
base.register('info', InfoElement);
base.register('contact', ContactElement);
base.register('license', LicenseElement);
Expand Down
22 changes: 22 additions & 0 deletions packages/apidom-ns-openapi-2/src/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createPredicate } from '@swagger-api/apidom-core';

import SwaggerElement from './elements/Swagger';
import SwaggerVersionElement from './elements/SwaggerVersion';
import InfoElement from './elements/Info';
import ContactElement from './elements/Contact';
import LicenseElement from './elements/License';
Expand All @@ -26,6 +28,26 @@ import SecuritySchemeElement from './elements/SecurityScheme';
import SecurityRequirementElement from './elements/SecurityRequirement';
import ScopesElement from './elements/Scopes';

export const isSwaggerElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is SwaggerElement =>
element instanceof SwaggerElement ||
(hasBasicElementProps(element) &&
isElementType('swagger', element) &&
primitiveEq('object', element));
},
);

export const isSwaggerVersionElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is SwaggerVersionElement =>
element instanceof SwaggerVersionElement ||
(hasBasicElementProps(element) &&
isElementType('swaggerVersion', element) &&
primitiveEq('string', element));
},
);

export const isInfoElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is InfoElement =>
Expand Down
18 changes: 18 additions & 0 deletions packages/apidom-ns-openapi-2/src/refractor/registration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import SwaggerElement from '../elements/Swagger';
import SwaggerVersionElement from '../elements/SwaggerVersion';
import InfoElement from '../elements/Info';
import ContactElement from '../elements/Contact';
import LicenseElement from '../elements/License';
Expand Down Expand Up @@ -26,6 +28,20 @@ import SecurityRequirementElement from '../elements/SecurityRequirement';
import { createRefractor } from './index';

// register refractors specific to element types
SwaggerElement.refract = createRefractor([
'visitors',
'document',
'objects',
'Swagger',
'$visitor',
]);
SwaggerVersionElement.refract = createRefractor([
'visitors',
'document',
'objects',
'SwaggerVersion',
'$visitor',
]);
InfoElement.refract = createRefractor(['visitors', 'document', 'objects', 'Info', '$visitor']);
ContactElement.refract = createRefractor([
'visitors',
Expand Down Expand Up @@ -155,6 +171,8 @@ SecurityRequirementElement.refract = createRefractor([
]);

export {
SwaggerElement,
SwaggerVersionElement,
InfoElement,
ContactElement,
LicenseElement,
Expand Down
Loading