Skip to content

Commit

Permalink
fix(specmap): provide multi platform backward compatible relative URL…
Browse files Browse the repository at this point in the history
… Reference resolution (#3150)

Refs #3149
  • Loading branch information
char0n authored Sep 18, 2023
1 parent cd84a30 commit 7a5bc77
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// eslint-disable-next-line import/prefer-default-export
export const ACCEPT_HEADER_VALUE_FOR_DOCUMENTS = 'application/json, application/yaml';

export const DEFAULT_BASE_URL = 'https://swagger.io';
3 changes: 2 additions & 1 deletion src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cookie from 'cookie';
import { isPlainObject } from 'is-plain-object';
import { url } from '@swagger-api/apidom-reference/configuration/empty';

import { DEFAULT_BASE_URL } from '../constants.js';
import stockHttp, { mergeInQueryOrForm } from '../http/index.js';
import createError from '../specmap/lib/create-error.js';
import SWAGGER2_PARAMETER_BUILDERS from './swagger2/parameter-builders.js';
Expand All @@ -22,7 +23,7 @@ const parseURIReference = (uriReference) => {
try {
return new URL(uriReference);
} catch {
const parsedURL = new URL(uriReference, 'https://swagger.io');
const parsedURL = new URL(uriReference, DEFAULT_BASE_URL);
const pathname = String(uriReference).startsWith('/')
? parsedURL.pathname
: parsedURL.pathname.substring(1);
Expand Down
3 changes: 2 additions & 1 deletion src/resolver/strategies/openapi-3-1-apidom/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import BinaryParser from '@swagger-api/apidom-reference/parse/parsers/binary';
import OpenApi3_1ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-1';

import { DEFAULT_BASE_URL } from '../../../constants.js';
import * as optionsUtil from '../../utils/options.js';
import normalize from './normalize.js';
import HttpResolverSwaggerClient from '../../apidom/reference/resolve/resolvers/http-swagger-client/index.js';
Expand Down Expand Up @@ -44,7 +45,7 @@ const resolveOpenAPI31Strategy = async (options) => {
const { cache } = resolveOpenAPI31Strategy;

// determining BaseURI
const cwd = url.isHttpUrl(url.cwd()) ? url.cwd() : 'https://smartbear.com/';
const cwd = url.isHttpUrl(url.cwd()) ? url.cwd() : DEFAULT_BASE_URL;
const retrievalURI = optionsUtil.retrievalURI(options);
const baseURI = url.resolve(cwd, retrievalURI);

Expand Down
8 changes: 5 additions & 3 deletions src/specmap/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import traverse from 'traverse';
import { url } from '@swagger-api/apidom-reference/configuration/empty';

import { DEFAULT_BASE_URL } from '../constants.js';

// This will match if the direct parent's key exactly matches an item.
const freelyNamedKeyParents = ['properties'];

Expand Down Expand Up @@ -78,10 +80,10 @@ export function absolutifyPointer(pointer, baseUrl) {
let newRefUrlPart;

if (!url.isHttpUrl(safeBaseUrl)) {
const cwd = url.cwd();
const absoluteBaseUrl = url.resolve(cwd, safeBaseUrl);
const absoluteBaseUrl = url.resolve(DEFAULT_BASE_URL, safeBaseUrl);
const absoluteRefUrlPart = url.resolve(absoluteBaseUrl, safeUrlPart);
newRefUrlPart = absoluteRefUrlPart.replace(cwd, '');
const rawRefUrlPart = absoluteRefUrlPart.replace(DEFAULT_BASE_URL, '');
newRefUrlPart = safeUrlPart.startsWith('/') ? rawRefUrlPart : rawRefUrlPart.substring(1);
} else {
newRefUrlPart = url.resolve(safeBaseUrl, safeUrlPart);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
},
"Pets": {
"items": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
"$$ref": "https://swagger.io/petstore.json#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand Down Expand Up @@ -1088,9 +1088,9 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pets",
"$$ref": "https://swagger.io/petstore.json#/components/schemas/Pets",
"items": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
"$$ref": "https://swagger.io/petstore.json#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand Down Expand Up @@ -1128,7 +1128,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
"$$ref": "https://swagger.io/petstore.json#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
Expand Down Expand Up @@ -1169,7 +1169,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
"$$ref": "https://swagger.io/petstore.json#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
Expand Down Expand Up @@ -1225,7 +1225,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
"$$ref": "https://swagger.io/petstore.json#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand All @@ -1252,7 +1252,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
"$$ref": "https://swagger.io/petstore.json#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
Expand Down Expand Up @@ -1763,7 +1763,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
},
"Pets": {
"items": {
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
"$$ref": "https://swagger.io/#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand Down Expand Up @@ -1817,9 +1817,9 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/#/components/schemas/Pets",
"$$ref": "https://swagger.io/#/components/schemas/Pets",
"items": {
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
"$$ref": "https://swagger.io/#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand Down Expand Up @@ -1857,7 +1857,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/#/components/schemas/Error",
"$$ref": "https://swagger.io/#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
Expand Down Expand Up @@ -1898,7 +1898,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/#/components/schemas/Error",
"$$ref": "https://swagger.io/#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
Expand Down Expand Up @@ -1954,7 +1954,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
"$$ref": "https://swagger.io/#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand All @@ -1981,7 +1981,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/#/components/schemas/Error",
"$$ref": "https://swagger.io/#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports[`subtree-resolver OpenAPI 3.1.0 strategy should not resolve an untargete
},
"Pets": {
"items": {
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
"$$ref": "https://swagger.io/#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand Down Expand Up @@ -233,7 +233,7 @@ exports[`subtree-resolver OpenAPI 3.1.0 strategy should resolve a subtree of an
"errors": [],
"spec": {
"items": {
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
"$$ref": "https://swagger.io/#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand Down

0 comments on commit 7a5bc77

Please sign in to comment.