From 92c1b72fcd3ce30c119696f03b76e93ab3606ff2 Mon Sep 17 00:00:00 2001 From: Mehtab Singh Date: Sun, 2 Feb 2025 02:48:26 +0530 Subject: [PATCH] Fix #1075: Added server.title() hasTitle() functions - title(): returns the server title string or undefined - hasTitle: returns boolean indicating if title exists - added the unit tests for the respective functions - tested the code by building, ensuring changes in esm, cjs --- packages/multi-parser/package.json | 2 +- packages/parser/src/models/server.ts | 2 ++ packages/parser/src/models/v2/server.ts | 8 ++++++++ packages/parser/src/spec-types/v2.ts | 1 + packages/parser/test/models/v2/server.spec.ts | 17 +++++++++++++++++ packages/parser/test/models/v3/server.spec.ts | 17 +++++++++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/multi-parser/package.json b/packages/multi-parser/package.json index 4755b5087..0f2ea5b9e 100644 --- a/packages/multi-parser/package.json +++ b/packages/multi-parser/package.json @@ -32,7 +32,7 @@ "test": "npm run test:unit", "test:unit": "cross-env CI=true jest --coverage", "lint": "eslint --max-warnings 0 --config ../../.eslintrc --ignore-path ../../.eslintignore .", - "lint:fix": "eslint --max-warnings 0 --config .../../eslintrc --ignore-path ../../.eslintignore . --fix", + "lint:fix": "eslint --max-warnings 0 --config ../../.eslintrc --ignore-path ../../.eslintignore . --fix", "generate:readme:toc": "markdown-toc -i \"../../README.md\"", "generate:assets": "npm run generate:readme:toc", "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION", diff --git a/packages/parser/src/models/server.ts b/packages/parser/src/models/server.ts index 52e2ece50..a24f434ea 100644 --- a/packages/parser/src/models/server.ts +++ b/packages/parser/src/models/server.ts @@ -12,6 +12,8 @@ export interface ServerInterface extends BaseModel, DescriptionMixinInterface, B host(): string; hasPathname(): boolean; pathname(): string | undefined; + title(): string | undefined; + hasTitle(): boolean; protocol(): string; protocolVersion(): string | undefined; hasProtocolVersion(): boolean; diff --git a/packages/parser/src/models/v2/server.ts b/packages/parser/src/models/v2/server.ts index 10521fad2..c86c8e630 100644 --- a/packages/parser/src/models/v2/server.ts +++ b/packages/parser/src/models/v2/server.ts @@ -49,6 +49,14 @@ export class Server extends BaseModel implement return url.pathname; } + title(): string | undefined { + return this._json.title; + } + + hasTitle(): boolean { + return !!this._json.title; + } + protocol(): string { return this._json.protocol; } diff --git a/packages/parser/src/spec-types/v2.ts b/packages/parser/src/spec-types/v2.ts index 758cd3035..3fa5a8e05 100644 --- a/packages/parser/src/spec-types/v2.ts +++ b/packages/parser/src/spec-types/v2.ts @@ -41,6 +41,7 @@ export type ServersObject = Record; export interface ServerObject extends SpecificationExtensions { url: string; protocol: string; + title?: string; protocolVersion?: string; description?: string; variables?: Record; diff --git a/packages/parser/test/models/v2/server.spec.ts b/packages/parser/test/models/v2/server.spec.ts index bb98877ba..853840832 100644 --- a/packages/parser/test/models/v2/server.spec.ts +++ b/packages/parser/test/models/v2/server.spec.ts @@ -16,6 +16,7 @@ import { SecurityRequirement } from '../../../src/models/v2/security-requirement const doc = { development: { + title: 'mqtt development server', protocol: 'mqtt', protocolVersion: '1.0.0', url: 'development.gigantic-server.com', @@ -37,6 +38,22 @@ describe('Server Model', function () { }); }); + describe('.title()', function () { + it('should return title', function () { + expect(docItem.title()).toMatch(doc.development.title); + }); + }); + + describe('.hasTitle()', function () { + it('should return true if title is present', function () { + expect(docItem.hasTitle()).toBeTruthy(); + }); + + it('should return false if title is missing', function () { + expect(emptyItem.hasTitle()).toBeFalsy(); + }); + }); + describe('protocol()', function () { it('should return protocol ', function () { expect(docItem.protocol()).toMatch(doc.development.protocol); diff --git a/packages/parser/test/models/v3/server.spec.ts b/packages/parser/test/models/v3/server.spec.ts index 2669bc8dd..9a11e3596 100644 --- a/packages/parser/test/models/v3/server.spec.ts +++ b/packages/parser/test/models/v3/server.spec.ts @@ -18,6 +18,7 @@ import { xParserObjectUniqueId } from '../../../src/constants'; const doc = { production: { host: 'rabbitmq.in.mycompany.com:5672', + title: 'rabbitmq production server', pathname: '/production', protocol: 'amqp', protocolVersion: '1.0.0', @@ -51,6 +52,22 @@ describe('Server Model', function () { }); }); + describe('.title()', function () { + it('should return title', function () { + expect(docItem.title()).toEqual(doc.production.title); + }); + }); + + describe('.hasTitle()', function () { + it('should return true if title is present', function () { + expect(docItem.hasTitle()).toBeTruthy(); + }); + + it('should return false if title is missing', function () { + expect(emptyItem.hasTitle()).toBeFalsy(); + }); + }); + describe('protocol()', function () { it('should return protocol ', function () { expect(docItem.protocol()).toEqual(doc.production.protocol);