Skip to content

feat(python): add Literal type support for const properties in Pydantic preset#2388

Merged
jonaslagoni merged 4 commits intoasyncapi:masterfrom
varundeepsaini:feat/pydantic-literal-const-support
Dec 5, 2025
Merged

feat(python): add Literal type support for const properties in Pydantic preset#2388
jonaslagoni merged 4 commits intoasyncapi:masterfrom
varundeepsaini:feat/pydantic-literal-const-support

Conversation

@varundeepsaini
Copy link
Contributor

Description

Add Literal type support for const properties in the Pydantic Python preset.

Previously, when a JSON Schema field was declared with the const keyword, the Pydantic preset generated a plain type:

class AnonymousSchema1(BaseModel): 
  country: str = Field()

Now it correctly generates a Literal type:

class AnonymousSchema1(BaseModel): 
  country: Literal["United States of America"] = Field(default='United States of America', frozen=True)

This implementation handles:

  • String const values → Literal['value']
  • Number const values → Literal[42]
  • Boolean const values → Literal[True] / Literal[False]

Related Issue

Fixes #2225

Checklist

  • The code follows the project's coding standards and is properly linted (npm run lint).
  • Tests have been added or updated to cover the changes.
  • Documentation has been updated to reflect the changes.
  • All tests pass successfully locally. (npm run test).

@netlify
Copy link

netlify bot commented Dec 5, 2025

Deploy Preview for modelina ready!

Name Link
🔨 Latest commit c3e747c
🔍 Latest deploy log https://app.netlify.com/projects/modelina/deploys/69330ca732fd5c0008db88be
😎 Deploy Preview https://deploy-preview-2388--modelina.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coveralls
Copy link

coveralls commented Dec 5, 2025

Pull Request Test Coverage Report for Build 19969736921

Details

  • 25 of 25 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.007%) to 91.026%

Totals Coverage Status
Change from base Build 19961633573: 0.007%
Covered Lines: 6943
Relevant Lines: 7420

💛 - Coveralls

@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 5, 2025

@jonaslagoni jonaslagoni merged commit 05b1d59 into asyncapi:master Dec 5, 2025
16 checks passed
@varundeepsaini varundeepsaini deleted the feat/pydantic-literal-const-support branch January 4, 2026 16:46
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 6.0.0-next.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Literal support for pydantic python preset, when field in jsonschema declared with const keyword

4 participants