-
Notifications
You must be signed in to change notification settings - Fork 9.2k
feat: display numeric constraints for path and query parameters #10586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: display numeric constraints for path and query parameters #10586
Conversation
Add formatNumericConstraints function to display minimum, maximum, exclusiveMinimum, and exclusiveMaximum constraints for path and query parameters in the documentation. - Add constraint formatting with proper mathematical symbols (, , >, <) - Support range notation for combined constraints [min, max], (min, max) - Display constraints in parameter description area - Add comprehensive unit tests for all constraint types - Work in both documentation view and Try it out mode
e7d5f21
to
f22d157
Compare
Hi @SiddarthaKarri, Thanks for your contribution! From what I’m seeing I would recommend the following steps for handling these cases:
Optional steps:
![]() |
- Replace custom formatNumericConstraints with plugin-exposed functions - Add stringifyConstraintNumberRange to json-schema-5 plugin for OpenAPI 2.0/3.0 - Expose existing stringifyConstraintNumberRange from json-schema-2020-12 plugin for OpenAPI 3.1 - Update ParameterRow to use appropriate constraint function based on OpenAPI version - Handle Immutable to plain object conversion for OpenAPI 3.1 - Add comprehensive unit tests for both constraint functions and component integration Addresses feedback from @glowcloud in PR review: - Uses existing plugin architecture instead of duplicating logic - Properly handles boolean vs number exclusiveMinimum/exclusiveMaximum - Exposes functions through fn.stringifyConstraintNumberRange and fn.jsonSchema202012.stringifyConstraintNumberRange
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on the refactor! Some additional changes are needed but the logic is sound. Please take a look at the new comments.
const isOAS31 = specSelectors.isOAS31 && specSelectors.isOAS31() | ||
let numericConstraint = null | ||
|
||
if (isOAS31 && fn.jsonSchema202012?.stringifyConstraintNumberRange) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be avoided by overriding the function for OpenAPI 3.1 here, the same way it was done for these functions. The existing wrapper should identify if our specification is in version 3.1.x and use the correct function.
: null | ||
} | ||
|
||
{(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's instead define the numericConstraint
before the return, e.g.
const numericConstraint = fn.stringifyConstraintNumberRange(schema)
return (
// ...
{numericConstraint && <Markdown className="parameter__constraint" source={`<i>Constraints</i>: ${numericConstraint}`}/>}
// ...
)
return hasType(schemaType) | ||
} | ||
|
||
export { stringifyConstraintNumberRange } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can instead simply export where the function is defined:
export const stringifyConstraintNumberRange = (schema) => {
|
||
// For OpenAPI 2.0 and 3.0 (JSON Schema Draft 4/5) | ||
// where exclusiveMinimum and exclusiveMaximum are booleans | ||
export const stringifyConstraintNumberRange = (schema) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid checking schema?.get
every time, we could instead either transform schema
to a plain object at the start with immutableToJS or have a helper function that will call this one after using immutableToJS
. An example of the second option is here.
import { stringifyConstraintNumberRange as stringifyConstraintNumberRangeDraft5 } from "core/plugins/json-schema-5/fn" | ||
|
||
describe("stringifyConstraintNumberRange", () => { | ||
describe("JSON Schema 2020-12 (OpenAPI 3.1)", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's split the tests into separate files and put them under
- test/unit/core/plugins/json-schema-5/fn
- test/unit/core/plugins/json-schema-2020-12/fn
isOAS3: () => isOAS3, | ||
isSwagger2: () => !isOAS3, | ||
}, | ||
fn: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to provide the correct fn.stringifyConstraintNumberRange
, based on the specification version, through here and test the display in these tests instead of parameter-constraints.spec.js
?
feat: display numeric constraints for path and query parameters
Description
This PR adds support for displaying numeric constraints (minimum, maximum, exclusiveMinimum, exclusiveMaximum) for path and query parameters in Swagger UI documentation. Previously, these constraints were only visible for request body schemas but not for parameters, making it unclear to API consumers what numeric boundaries are enforced.
The implementation adds a
formatNumericConstraints()
function that formats constraints with proper mathematical symbols and displays them in the parameter description area alongside existing parameter information like default values and examples.Motivation and Context
This change is required to improve API documentation clarity and developer experience. API consumers need to see parameter constraints to understand what values are valid without having to refer to external documentation or discover limits through trial and error.
Fixes #10562
How Has This Been Tested?
Manual Testing:
Test Environment:
Automated Testing:
Screenshots (if appropriate):
The constraints now appear in parameter documentation as:
Before: Only showed parameter name, type, and description
After: Shows constraints like "Constraints: ≥ 1" or "Constraints: [1, 100]" below the description
Examples of constraint display:
minimum: 1
→ "≥ 1"maximum: 100
→ "≤ 100"exclusiveMinimum: 0
→ "> 0"minimum: 1, maximum: 100
→ "[1, 100]"Checklist
My PR contains...
src/
is unmodified: changes to documentation, CI, metadata, etc.)package.json
)My changes...
Documentation
Automated tests