Skip to content

Support not converting const to enum#877

Merged
Eomm merged 1 commit intofastify:mainfrom
TastyPi:tastypi/const
Nov 8, 2025
Merged

Support not converting const to enum#877
Eomm merged 1 commit intofastify:mainfrom
TastyPi:tastypi/const

Conversation

@TastyPi
Copy link
Contributor

@TastyPi TastyPi commented May 30, 2025

Provides a new convertConstToEnum option in order to fix #876.

I had to pipe the plugin options throughout all the functions in openapi/util.js, let me know if there was a better way to do that.

Checklist

  • run npm run test and npm run benchmark

npm run benchmark does not seem to exist? npm run test succeeds.

@mcollina
Copy link
Member

Sorry for being late. This now conflicts.

@TastyPi
Copy link
Contributor Author

TastyPi commented Jul 31, 2025

@mcollina rebased, PTAL

@Fdawgs Fdawgs requested a review from Copilot November 6, 2025 20:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds a convertConstToEnum configuration option to control whether JSON Schema const keywords are automatically converted to enum in OpenAPI specifications. The const keyword was added in OpenAPI 3.1 but is not supported by all tools (like swagger-ui), so this option allows users to preserve const when using OpenAPI 3.1.0+ or convert it to enum for compatibility.

Key changes:

  • Added convertConstToEnum option with a default value of true for backward compatibility
  • Modified schema conversion logic to conditionally convert const to enum based on the option
  • Added comprehensive tests for both behaviors (preserving const and converting to enum)

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
lib/mode/dynamic.js Sets default value of convertConstToEnum to true
lib/spec/openapi/utils.js Adds convertConstToEnum to options flow and implements conditional conversion logic
lib/spec/openapi/index.js Passes options through to utility functions
index.d.ts Adds TypeScript type definition and documentation for the new option
test/spec/openapi/schema.test.js Adds tests for both preserving const and converting to enum

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update the readme as well

@TastyPi
Copy link
Contributor Author

TastyPi commented Nov 7, 2025

@Eomm Updated the README

@TastyPi TastyPi requested a review from Eomm November 7, 2025 13:29
@Eomm Eomm merged commit 1f70431 into fastify:main Nov 8, 2025
14 checks passed
@TastyPi TastyPi mentioned this pull request Nov 10, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI 3.1.x supports const, @fastify/swagger converts consts to enums

4 participants