Skip to content

Conversation

leandrodamascena
Copy link
Contributor

@leandrodamascena leandrodamascena commented Oct 5, 2024

Issue number: #5303

Summary

Changes

In Pydantic v2, when validating a model that has a field with a type of another model can break the validation. This limitation/bug in Pydantic is causing problems for customers trying to validate JSON against complex model structures

To work around this issue, we are modifying the validation code to use a try/except block. If a NotImplementedError is raised during JSON validation, we can fall back to using validate_python instead.

I added additional tests to make sure we are covering this part of code and also a e2e test.

User experience

import json
from typing import Any, Dict, Type, Union

from pydantic import BaseModel

from aws_lambda_powertools.utilities.parser import parse
from aws_lambda_powertools.utilities.typing import LambdaContext

AnyInheritedModel = Union[Type[BaseModel], BaseModel]
RawDictOrModel = Union[Dict[str, Any], AnyInheritedModel]

#payload = {"name": "powertools", "profile": {"description": "python", "size": "XXL"}}

class ModelWithUnionType(BaseModel):
    name: str
    profile: RawDictOrModel


def lambda_handler(event: ModelWithUnionType, context: LambdaContext):
    event = json.dumps(event)

    event_parsed = parse(event=event, model=ModelWithUnionType)

    return {"name": event_parsed.name}

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team October 5, 2024 21:18
@boring-cyborg boring-cyborg bot added the tests label Oct 5, 2024
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 5, 2024
@github-actions github-actions bot added the bug Something isn't working label Oct 5, 2024
@leandrodamascena leandrodamascena changed the title fix(parser): use validation_python to validate models with type model fix(parser): fallback to validate_python for type[Model] and nested models Oct 5, 2024
@leandrodamascena leandrodamascena changed the title fix(parser): fallback to validate_python for type[Model] and nested models fix(parser): fallback to validate_python when using type[Model] and nested models Oct 5, 2024
Copy link

codecov bot commented Oct 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.20%. Comparing base (ece20f8) to head (267e159).
Report is 3 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5313   +/-   ##
========================================
  Coverage    96.20%   96.20%           
========================================
  Files          229      229           
  Lines        10753    10761    +8     
  Branches      2002     2001    -1     
========================================
+ Hits         10345    10353    +8     
  Misses         322      322           
  Partials        86       86           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

sonarqubecloud bot commented Oct 7, 2024

@leandrodamascena leandrodamascena merged commit fe6b335 into develop Oct 7, 2024
10 checks passed
@leandrodamascena leandrodamascena deleted the fix/parser-json-validation branch October 7, 2024 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: The pydantic parser breaks when parsing JSON with a model containing a field with the type RawDictOrModel
2 participants