Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

OAS 3.1.0 output: maxLength constraint not saved to schema for nullable string field #1302

Closed
bartvanandel opened this issue Sep 30, 2024 · 3 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@bartvanandel
Copy link

Describe the bug
We have several string fields in our API that are nullable (i.e., optional), but also have a max length. When using OAS 3.0.3 as the target schema version, this works fine, however with OAS 3.1.0, no maxLength is stored. For non-nullable fields, maxLength is stored properly.

I expect more constraints (e.g., minLength should be obvious) are also dropped from the schema. Format specifiers (such as type: email), readOnly, writeOnly and probably other specs work fine for nullable fields across OpenAPI versions, so there must be something special about constraint handling.

To Reproduce

Python code:

# Inside some model:
class Team(models.Model):
    phone_number = models.CharField(null=True, blank=True, max_length=40)

Excerpt from generated OAS 3.0.3 schema:

    TeamRequest:
      type: object
      properties:
        phone_number:
          type: string
          nullable: true
          maxLength: 40

Same excerpt from generated OAS 3.1.0 schema:

    TeamRequest:
      type: object
      properties:
        phone_number:
          type:
          - string
          - 'null'

Expected behavior
Expected output should include maxLength constraint.
If I understand the OpenAPI specs correctly (hints taken from this OpenAPI Spec question), this concise form should be fine:

    TeamRequest:
      type: object
      description: Team info
      properties:
        phone_number:
          type:
          - string
          - 'null'
          maxLength: 40

If you want to be more explicit, this should work as well:

    TeamRequest:
      type: object
      description: Team info
      properties:
        phone_number:
          oneOf:
          - type: string
            maxLength: 40
          - 'null'
@bartvanandel bartvanandel changed the title OAS 3.1: maxLength not saved to schema for nullable string field OAS 3.1.0 output: maxLength constraint not saved to schema for nullable string field Sep 30, 2024
@tfranzel tfranzel added bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Oct 1, 2024
tfranzel added a commit that referenced this issue Oct 1, 2024
@bartvanandel
Copy link
Author

Thanks for picking this up, your fix looks fine to me.

Is there something up with Codecov though? That one task has been pending for over 9 days now on your commit.

@tfranzel
Copy link
Owner

codecov stalls sometimes. I have updated the GH action and did a fresh setup procedure. It worked a couple of times but it is still not 100% right. idk, this has worked flawlessly for years. we certainly didn't change anything.

@bartvanandel
Copy link
Author

Excuse the pause. I've just tested this locally, with our actual API, and your fix works fine indeed.

Excerpt from before the fix:

website:
  type:
  - string
  - 'null'
  format: uri

With fix applied:

website:
  type:
  - string
  - 'null'
  format: uri
  maxLength: 250

So, clear for take-off I guess! 🚀

@tfranzel tfranzel closed this as completed Dec 1, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants