fastify / fastify-response-validation

A simple plugin that enables response validation for Fastify.
MIT License
53 stars 17 forks source link

The openapi/swagger standard defines response schema keys as using "XX" in upper-case and additionally supports "default" response. #124

Closed n3rdyme closed 2 weeks ago

n3rdyme commented 3 weeks ago

Prerequisites

Fastify version

5.0.0

Plugin version

3.0.1

Node.js version

22.10.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Ubuntu 24.04

Description

The response validator is throwing exceptions when unable to find response code if using responseStatusCodeValidation: true. This is expected; however, the plugin is incorrectly checking for 4xx when pattern matching, which should be defined as 4XX. Additionally the plugin does not consider the default response when validating.

Relevant text from the OpenAPI standard:

Field Name Type Description
default Response Object Reference Object The documentation of responses other than the ones declared for specific HTTP response codes. Use this field to cover undeclared responses.
-- -- --
HTTP Status Code Response Object Reference Object Any HTTP status code can be used as the property name, but only one property per code, to describe the expected response for that HTTP status code. This field MUST be enclosed in quotation marks (for example, "200") for compatibility between JSON and YAML. To define a range of response codes, this field MAY contain the uppercase wildcard character X. For example, 2XX represents all response codes between [200-299]. Only the following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX. If a response is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code.

The documentation from the OpenAPI-Specification can be found here: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#responses-object

The documentation for swagger is the same and can be found here: https://swagger.io/specification/#fixed-fields-14

Link to code that reproduces the bug

See Test Cases In PR

Expected Behavior

Expected behavior matching the defined standard.

n3rdyme commented 1 week ago

@Fdawgs I see the PR was merged, thank you.

What are the plans for when this makes it to an npm version?

Fdawgs commented 1 week ago

@n3rdyme I can do one tomorrow. 😊

n3rdyme commented 1 week ago

@Fdawgs Flawless Victory 👍

Thank you so much :)