Skip to content

Commit

Permalink
fix: create default OpenAPI.servers fields if omitted (#3219)
Browse files Browse the repository at this point in the history
This change is specific to OpenAPI 3.x.y definitions.

Refs #3143
  • Loading branch information
char0n authored Nov 2, 2023
1 parent b113c23 commit 781077a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as getOperationRaw } from './get-operation-raw.js';
export { default as opId } from './op-id.js';
export { default as idFromPathMethod } from './id-from-path-method/index.js';
export { default as idFromPathMethodLegacy } from './id-from-path-method/legacy.js';
export { default as isHttpUrl } from './is-http-url.js';
5 changes: 5 additions & 0 deletions src/helpers/is-http-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { url } from '@swagger-api/apidom-reference/configuration/empty';

const { isHttpUrl } = url;

export default isHttpUrl;
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import openApi30ResolveStrategy from './resolver/strategies/openapi-3-0/index.js
import openApi31ApiDOMResolveStrategy from './resolver/strategies/openapi-3-1-apidom/index.js';
import { makeApisTagOperation } from './interfaces.js';
import { execute, buildRequest, baseUrl } from './execute/index.js';
import { opId } from './helpers/index.js';
import { opId, isHttpUrl } from './helpers/index.js';
import { isOpenAPI2, isOpenAPI3 } from './helpers/openapi-predicates.js';
import HttpResolverSwaggerClient from './resolver/apidom/reference/resolve/resolvers/http-swagger-client/index.js';
import JsonParser from './resolver/apidom/reference/parse/parsers/json/index.js';
import YamlParser from './resolver/apidom/reference/parse/parsers/yaml-1-2/index.js';
Expand Down Expand Up @@ -131,8 +132,8 @@ Swagger.prototype = {
Swagger.prototype.applyDefaults = function applyDefaults() {
const { spec } = this;
const specUrl = this.url;
// TODO: OAS3: support servers here
if (specUrl && specUrl.startsWith('http')) {

if (isOpenAPI2(spec) && isHttpUrl(specUrl)) {
const parsed = new URL(specUrl);
if (!spec.host) {
spec.host = parsed.host;
Expand All @@ -143,6 +144,10 @@ Swagger.prototype.applyDefaults = function applyDefaults() {
if (!spec.basePath) {
spec.basePath = '/';
}
} else if (isOpenAPI3(spec) && isHttpUrl(specUrl)) {
if (!spec.servers) {
spec.servers = [{ url: specUrl }];
}
}
};

Expand Down

0 comments on commit 781077a

Please sign in to comment.