Variability in Swagger parameter declaration

Is it possible to specify input parameters in Swagger with multiple types?

For example: Consider an API that addresses resources using the URL http://localhost/tasks/{taskId}. However, each task contains both an integer ID and a string UUID. I would like to enable users to address the resource using either the ID or the UUID - making both http://localhost/tasks/123 and

http://localhost/tasks/51f12dbc-02e7-41a6-ab81-2381caea0176
URLs valid.

According to the Swagger documentation (https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#parameter-object), a parameter object can only have a single type

Type: Required. The parameter's type. Since the parameter is not located in the request body, it is limited to simple types (not objects). The value must be one of "string", "number", "integer", "boolean", "array" or "file".

So how can I define the input path parameter as both a string and an integer?

Answer №1

It is not possible to specify a parameter that belongs to multiple types in the Swagger specification.

In this scenario, consider using a string data type as a workaround. This way, you can represent both strings (e.g. "51f12dbc-02e7-41a6-ab81-2381caea0176") and integers (e.g. "123") in the path parameter, allowing the server to handle the data correctly.

Answer №2

Utilizing the power of OpenAPI 3.0, achieving this is made possible through the use of oneOf:

openapi: 3.0.0
...
paths:
  /tasks/{taskId}:
    parameters:
      - in: path
        name: taskId
        required: true
        schema:
          oneOf:
            - type: integer
              example: 123
            - type: string
              format: uuid
              example: 51f12dbc-02e7-41a6-ab81-2381caea0176

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to use jQuery to hide rows in a data table that contain a specific code, such as "1.1" or "1.2," that includes a

When using ajax, I pass data from two inputs to another page. These values are then used in a query to display the results in a data table on the main page within the success function. Now, I'm looking to implement a condition where any data containi ...

A guide to finding matching data in two collections with mongoose's aggregate method

My quiz application has two schemas: const quizSchema = new mongoose.Schema({ userID: { type: String, required: [true, 'User ID required'] } }); Response Schema - const responseSchema = new mongoose.Schema({ userID: { type: Str ...

FadeOut Television static on Canvas after a period of inactivity

Is there a way to smoothly fade out the TV noise after a specific timeout period? I found this code snippet on Stack Overflow that seems to address a similar issue: var canvas = document.getElementById('canvas'), ctx = canvas.getContext( ...

Error: The property 'length' cannot be read from an undefined parent causing Uncaught TypeError

Hey there, take a look at this cool stuff http://jsfiddle.net/J9Tza/ <form class="validation"> <div> <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" r ...

Unlocking the Secret: How to Bind a Global Touch Event Handler in Angular 4 to Prevent iOS Safari Page Drag

The issue at hand is that within my Angular 4 application, there is a D3.js chart that relies on user touch input for dragging a needle to a specific value. The drag functionality is triggered by 'touchstart', while the registration of the final ...

Prevent the propagation of a particular CSS class's inheritance

I need to make modifications to an existing HTML5 app that features two unique themes. Here's an example of the current structure: <body class="theme1 theme2"> <div id="div1">I'm satisfied with both themes</div> <di ...

`validate.js verifying the elements within an array`

One of the challenges I'm facing in my JavaScript project is dealing with objects that have two array properties included. As part of my development process, I've decided to utilize the resources provided by the validate.js library. To illustrat ...

Avoiding the occurrence of moiré patterns when using pixi.js

My goal is to create a zoomable canvas with rectangles arranged in a grid using pixi.js. Despite everything working smoothly, I'm facing heavy moire effects due to the grid. My understanding of pixi.js and webgl is limited, but I suspect that the anti ...

`npm security check reveals a potential vulnerability related to Inefficient Regular Expression Complexity. For more information, refer to: https://github.com/advisories/GHSA-9vvw-cc

# npm audit report debug <3.1.0 debug Inefficient Regular Expression Complexity vulnerability - https://github.com/advisories/GHSA-9vvw-cc9w-f27h No solution currently available node_modules/body-parser/node_modules/debug node_modules/express/node_modu ...

Tips for retrying an insertion into the database when a duplicate unique value is already present

After thorough searching, I couldn't find any existing patterns. My goal is to store a unique key value in my MySQL database. I generate it on the server side using this code: var pc = require('password-creator'); var key = pc.create(20); ...

Capturing information transmitted from an input device

I'm currently working on capturing input data from a piano connected to my computer via USB. $ lsusb ... Bus 003 Device 046: ID fc08:0101 .... The piano is identified as Bus 003 Device 046: ID fc08:0101. When I try $ cat /dev/bus/usb/003/046, th ...

Update the displayed number on the input field as a German-formatted value whenever there is a change in the input, all while maintaining

In German decimal numbers, a decimal comma is used. When formatting, periods are also used for grouping digits. For example, the number 10000.45 would be formatted as 10.000,45. I am looking to create an input field (numeric or text) where users can input ...

Exploring the possibilities of custom layouts for specific routes within the pages directory in Next.js

I am interested in incorporating layout-based routing within my project's pages directory. I want to find a way to have a specific file, like the _app.tsx, that can only affect the files located inside a particular folder. This setup would operate si ...

Capturing website links with Node.js asynchronously

My code snippet is as follows: const fs = require('fs'); const screenshot = require('screenshot-stream'); const urlp = require('url'); var urls=[ 'https://archive.org/details/8bitrecs', 'http://hackaday.com/&ap ...

Solving the Cross-Origin Resource Sharing problem in AngularJS

While using the http dependency in AngularJS and setting headers for CORS, I am encountering an error. Please check the console.log for more information on the error. The error message reads: "XMLHttpRequest cannot load . Response to preflight request doe ...

Ways to retrieve the current URL in Next.js without relying on window.location.href or React Router

Is there a way to fetch the current URL in Next.js without relying on window.location.href or React Router? const parse = require("url-parse"); parse("hostname", {}); console.log(parse.href); ...

Tips for automatically inserting a "read more" link once text exceeds a certain character count

Currently utilizing an open-source code to fetch Google reviews, but facing an issue with long reviews. They are messing up the layout of my site. I need to limit the characters displayed for each review and provide an option for users to read the full rev ...

transferring data between react components

I am currently working on breaking down code into smaller components, starting with the snippet below from Rows.jsx. I am struggling to grasp how props are passed between parent and child components. Despite reading several guides and articles, I remain un ...

Ajax is functional, however the server is not responding

After many attempts to resolve the issue with my website, I am turning to this community in hopes of finding a solution. The problem lies in the fact that while the ajax success function appears to be working and shows a status code of 200 in the network ...

Issues with Toggling Visibility in HTML, CSS, and Javascript

Currently, I am working on a website and following a tutorial called "Creating Slideshow using HTML, CSS, and Javascript" by W3Schools. In the project, I want to hide the thumbnail images located at the bottom and the navigation arrows initially and have t ...