Skip to content

Commit

Permalink
fix(resolver): add relative reference support for baseDoc option (#2905)
Browse files Browse the repository at this point in the history
This change is specific to OpenAPI 3.1.0
resolver strategy.

Refs swagger-api/swagger-ui#8525
  • Loading branch information
char0n authored Mar 30, 2023
1 parent 138e982 commit ddd80db
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/resolver/strategies/openapi-3-1-apidom/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const resolveOpenAPI31Strategy = async (options) => {
} = options;
try {
// determining BaseURI
const defaultBaseURI = 'https://smartbear.com/';
const retrievalURI = optionsUtil.retrievalURI(options) ?? url.cwd();
const baseURI = url.isHttpUrl(retrievalURI) ? retrievalURI : defaultBaseURI;
const cwd = url.isHttpUrl(url.cwd()) ? url.cwd() : 'https://smartbear.com/';
const retrievalURI = optionsUtil.retrievalURI(options);
const baseURI = url.resolve(cwd, retrievalURI);

// prepare spec for dereferencing
const openApiElement = OpenApi3_1Element.refract(spec);
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const retrievalURI = (options) => {

// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
// need to fix and pick one.
return baseDoc || url;
return baseDoc || url || '';
};

export const httpClient = (options) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,315 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via URL s
}
`;

exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and baseDoc option is provided and baseDoc option is a relative reference should resolve the definition with the resolved baseDoc 1`] = `
{
"errors": [],
"spec": {
"$$normalized": true,
"components": {
"schemas": {
"Error": {
"properties": {
"code": {
"format": "int32",
"type": "integer",
},
"message": {
"type": "string",
},
},
"required": [
"code",
"message",
],
"type": "object",
},
"Pet": {
"properties": {
"id": {
"format": "int64",
"type": "integer",
},
"name": {
"type": "string",
},
"tag": {
"type": "string",
},
},
"required": [
"id",
"name",
],
"type": "object",
},
"Pets": {
"items": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
"type": "integer",
},
"name": {
"type": "string",
},
"tag": {
"type": "string",
},
},
"required": [
"id",
"name",
],
"type": "object",
},
"maxItems": 100,
"type": "array",
},
},
},
"info": {
"license": {
"name": "MIT",
},
"title": "Swagger Petstore",
"version": "1.0.0",
},
"openapi": "3.1.0",
"paths": {
"/pets": {
"get": {
"operationId": "listPets",
"parameters": [
{
"description": "How many items to return at one time (max 100)",
"in": "query",
"name": "limit",
"required": false,
"schema": {
"format": "int32",
"maximum": 100,
"type": "integer",
},
},
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pets",
"items": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
"type": "integer",
},
"name": {
"type": "string",
},
"tag": {
"type": "string",
},
},
"required": [
"id",
"name",
],
"type": "object",
},
"maxItems": 100,
"type": "array",
},
},
},
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string",
},
},
},
},
"default": {
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
"type": "integer",
},
"message": {
"type": "string",
},
},
"required": [
"code",
"message",
],
"type": "object",
},
},
},
"description": "unexpected error",
},
},
"servers": [
{
"url": "http://petstore.swagger.io/v1",
},
],
"summary": "List all pets",
"tags": [
"pets",
],
},
"post": {
"operationId": "createPets",
"responses": {
"201": {
"description": "Null response",
},
"default": {
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
"type": "integer",
},
"message": {
"type": "string",
},
},
"required": [
"code",
"message",
],
"type": "object",
},
},
},
"description": "unexpected error",
},
},
"servers": [
{
"url": "http://petstore.swagger.io/v1",
},
],
"summary": "Create a pet",
"tags": [
"pets",
],
},
"servers": [
{
"url": "http://petstore.swagger.io/v1",
},
],
},
"/pets/{petId}": {
"get": {
"operationId": "showPetById",
"parameters": [
{
"description": "The id of the pet to retrieve",
"in": "path",
"name": "petId",
"required": true,
"schema": {
"type": "string",
},
},
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
"type": "integer",
},
"name": {
"type": "string",
},
"tag": {
"type": "string",
},
},
"required": [
"id",
"name",
],
"type": "object",
},
},
},
"description": "Expected response to a valid request",
},
"default": {
"content": {
"application/json": {
"schema": {
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
"properties": {
"code": {
"format": "int32",
"type": "integer",
},
"message": {
"type": "string",
},
},
"required": [
"code",
"message",
],
"type": "object",
},
},
},
"description": "unexpected error",
},
},
"servers": [
{
"url": "http://petstore.swagger.io/v1",
},
],
"summary": "Info for a specific pet",
"tags": [
"pets",
],
},
"servers": [
{
"url": "http://petstore.swagger.io/v1",
},
],
},
},
"servers": [
{
"url": "http://petstore.swagger.io/v1",
},
],
},
}
`;

exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and baseDoc option is provided should resolve 1`] = `
{
"errors": [],
Expand Down Expand Up @@ -1410,7 +1719,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
}
`;

exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and neither baseDoc nor url option is provided should resolve 1`] = `
exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and neither baseDoc nor url option is provided should resolve using implicit baseURI=https://smartbear.com/ 1`] = `
{
"errors": [],
"spec": {
Expand Down Expand Up @@ -1454,6 +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",
"properties": {
"id": {
"format": "int64",
Expand Down Expand Up @@ -1507,7 +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",
"items": {
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
"properties": {
"id": {
"format": "int64",
Expand Down Expand Up @@ -1545,6 +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",
"properties": {
"code": {
"format": "int32",
Expand Down Expand Up @@ -1585,6 +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",
"properties": {
"code": {
"format": "int32",
Expand Down Expand Up @@ -1640,6 +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",
"properties": {
"id": {
"format": "int64",
Expand All @@ -1666,6 +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",
"properties": {
"code": {
"format": "int32",
Expand Down
Loading

0 comments on commit ddd80db

Please sign in to comment.