Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Update Pydantic to v2.4.2 #22

Merged
merged 24 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5df9feb
feat: upgrade pydantic to 2.4.2
TheHelias Oct 9, 2023
1b88060
Merge pull request #1 from TheHelias/feat/pydantic-2
TheHelias Oct 9, 2023
b638ef1
fix: path issue on windows causing C-Input/C-Output
TheHelias Oct 13, 2023
41b6df9
update snapshots to fit new openapi version
TheHelias Oct 13, 2023
6ef3cb1
Merge pull request #2 from TheHelias/feat/pydantic-2
TheHelias Oct 16, 2023
ee4808a
feat: add github issue link
TheHelias Oct 25, 2023
b716132
fix: return ability to validate emails
TheHelias Oct 25, 2023
376abe5
remove unnecessary file
TheHelias Oct 25, 2023
e859ee5
fix: make error message typ strictly string
TheHelias Oct 30, 2023
bcb47e8
Merge pull request #3 from TheHelias/feat/pydantic-2
TheHelias Oct 30, 2023
708cb66
fix: auth token default type
TheHelias Oct 30, 2023
914327a
refactor: improve naming
TheHelias Oct 30, 2023
6f9cac0
fix: remove field users don't fill from definition model
TheHelias Oct 30, 2023
5695191
Merge pull request #4 from TheHelias/feat/pydantic-2
TheHelias Oct 30, 2023
521ad09
fix: load modules with sane name to rid of workaround
TheHelias Nov 1, 2023
25b2991
Merge pull request #5 from TheHelias/feat/pydantic-2
TheHelias Nov 1, 2023
7b8237e
fix: remove logging
TheHelias Nov 1, 2023
6077765
Merge pull request #6 from TheHelias/feat/pydantic-2
TheHelias Nov 1, 2023
84aba33
chore: update fastapi
TheHelias Nov 7, 2023
eb9671f
Merge pull request #7 from TheHelias/feat/pydantic-2
TheHelias Nov 7, 2023
7fad6d6
refactor: use semver __get_pydantic_core_schema__
TheHelias Nov 8, 2023
2b8812e
Merge pull request #8 from TheHelias/feat/pydantic-2
TheHelias Nov 8, 2023
7671345
chorwe: bump version
TheHelias Nov 8, 2023
5e07f12
Merge pull request #9 from TheHelias/feat/pydantic-2
TheHelias Nov 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions definition_tooling/converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

from deepdiff import DeepDiff
from fastapi import FastAPI, Header
from pydantic import BaseModel, ValidationError, conint, validator
from pydantic import BaseModel, ConfigDict, Field, ValidationError, field_validator
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import core_schema
from rich import print
from semver import Version
from stringcase import camelcase
from typing_extensions import Annotated

from definition_tooling.api_errors import DATA_PRODUCT_ERRORS


class CamelCaseModel(BaseModel):
class Config:
alias_generator = camelcase
allow_population_by_field_name = True
model_config = ConfigDict(alias_generator=camelcase, populate_by_name=True)


class ErrorModel(BaseModel):
Expand Down Expand Up @@ -63,7 +64,7 @@ def __call__(self, model_cls: Type[BaseModel]) -> ErrorModel:
)


ERROR_CODE = conint(ge=400, lt=600)
ERROR_CODE = Annotated[int, Field(ge=400, lt=600)]


class PydanticVersion(Version):
Expand All @@ -80,35 +81,36 @@ def _parse(cls, version):
return cls.parse(version)
TheHelias marked this conversation as resolved.
Show resolved Hide resolved
TheHelias marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def __get_validators__(cls):
def __get_pydantic_core_schema__(
cls, source_type, _handler
) -> core_schema.CoreSchema:
"""Return a list of validator methods for pydantic models."""
yield cls._parse
return core_schema.no_info_wrap_validator_function(
cls.parse,
core_schema.str_schema(),
serialization=core_schema.to_string_ser_schema(),
)

@classmethod
def __modify_schema__(cls, field_schema):
def __get_pydantic_json_schema__(cls, _core_schema, handler) -> JsonSchemaValue:
"""Inject/mutate the pydantic field schema in-place."""
field_schema.update(
examples=[
"1.0.2",
"2.15.3-alpha",
"21.3.15-beta+12345",
]
)
return handler(core_schema.str_schema())


class DataProductDefinition(BaseModel):
version: PydanticVersion = "0.0.1"
deprecated: bool = False
description: str
error_responses: Dict[ERROR_CODE, ErrorModel] = {}
name: Optional[str]
name: Optional[str] = None
request: Type[BaseModel]
requires_authorization: bool = False
requires_consent: bool = False
response: Type[BaseModel]
title: str

@validator("error_responses")
@field_validator("error_responses")
@classmethod
def validate_error_responses(cls, v: Dict[ERROR_CODE, ErrorModel]):
status_codes = set(v.keys())
reserved_status_codes = set(DATA_PRODUCT_ERRORS.keys())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
"properties": {
"lat": {
"description": "The latitude coordinate of the desired location",
"example": 60.192059,
"examples": [
60.192059
],
"maximum": 90.0,
"minimum": -90.0,
"title": "Latitude",
"type": "number"
},
"lon": {
"description": "The longitude coordinate of the desired location",
"example": 24.945831,
"examples": [
24.945831
],
"maximum": 180.0,
"minimum": -180.0,
"title": "Longitude",
Expand All @@ -37,15 +41,19 @@
"properties": {
"airQualityIndex": {
"description": "Current air quality index.\nRanges:\n0-50 Good;\n51-100 Moderate;\n101-150 Unhealthy For Sensitive Groups;\n151-200 Unhealthy;\n201-300 Very Unhealthy;\n301+ Hazardous",
"example": 30,
"examples": [
30
],
"minimum": 0.0,
"title": "Air Quality Index",
"type": "integer"
},
"attribution": {
"description": "List of text to show required credits to data sources",
"example": [
"Moscow State environmental monitoring"
"examples": [
[
"Moscow State environmental monitoring"
]
],
"items": {
"type": "string"
Expand All @@ -55,7 +63,9 @@
},
"timestamp": {
"description": "Current timestamp in RFC 3339 format",
"example": "2020-04-03T13:00:00Z",
"examples": [
"2020-04-03T13:00:00Z"
],
"title": "Timestamp",
"type": "string"
}
Expand Down Expand Up @@ -145,9 +155,16 @@
"description": "This response is reserved by Product Gateway.",
"properties": {
"message": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "Error description",
"title": "Error message",
"type": "string"
"title": "Error message"
}
},
TheHelias marked this conversation as resolved.
Show resolved Hide resolved
"title": "GatewayTimeout",
Expand Down Expand Up @@ -190,9 +207,16 @@
"description": "This response is reserved by Product Gateway.",
"properties": {
"message": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "Error description",
"title": "Error message",
"type": "string"
"title": "Error message"
TheHelias marked this conversation as resolved.
Show resolved Hide resolved
}
},
"title": "ServiceUnavailable",
Expand Down Expand Up @@ -258,7 +282,7 @@
"title": "Air Quality Index",
"version": "1.0.0"
},
"openapi": "3.0.2",
"openapi": "3.1.0",
"paths": {
"/AirQuality/Current": {
"post": {
Expand All @@ -271,9 +295,16 @@
"name": "x-consent-token",
"required": false,
"schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "Optional consent token",
"title": "X-Consent-Token",
"type": "string"
"title": "X-Consent-Token"
}
},
{
Expand All @@ -282,9 +313,16 @@
"name": "authorization",
"required": false,
"schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "The login token. Value should be \"Bearer [token]\"",
"title": "Authorization",
"type": "string"
"title": "Authorization"
}
},
{
Expand All @@ -293,9 +331,16 @@
"name": "x-authorization-provider",
"required": false,
"schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "The bare domain of the system that provided the token.",
"title": "X-Authorization-Provider",
"type": "string"
"title": "X-Authorization-Provider"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"properties": {
"companyId": {
"description": "The ID of the company",
"example": "2464491-9",
"examples": [
"2464491-9"
],
"title": "Company ID",
"type": "string"
}
Expand All @@ -25,22 +27,30 @@
"BasicCompanyInfoResponse": {
"properties": {
"companyForm": {
"example": "LLC",
"examples": [
"LLC"
],
"title": "The company form of the company",
"type": "string"
},
"companyId": {
"example": "2464491-9",
"examples": [
"2464491-9"
],
"title": "ID of the company",
"type": "string"
},
"name": {
"example": "Digital Living International Oy",
"examples": [
"Digital Living International Oy"
],
"title": "Name of the company",
"type": "string"
},
"registrationDate": {
"example": "2012-02-23",
"examples": [
"2012-02-23"
],
"title": "Date of registration for the company",
"type": "string"
}
Expand Down Expand Up @@ -131,9 +141,16 @@
"description": "This response is reserved by Product Gateway.",
"properties": {
"message": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "Error description",
"title": "Error message",
"type": "string"
"title": "Error message"
TheHelias marked this conversation as resolved.
Show resolved Hide resolved
}
},
"title": "GatewayTimeout",
Expand Down Expand Up @@ -163,9 +180,16 @@
"description": "This response is reserved by Product Gateway.",
"properties": {
"message": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "Error description",
"title": "Error message",
"type": "string"
"title": "Error message"
TheHelias marked this conversation as resolved.
Show resolved Hide resolved
}
},
"title": "ServiceUnavailable",
Expand Down Expand Up @@ -212,7 +236,7 @@
"title": "Information about a company",
"version": "1.0.0"
},
"openapi": "3.0.2",
"openapi": "3.1.0",
"paths": {
"/Company/BasicInfo": {
"post": {
Expand All @@ -225,9 +249,16 @@
"name": "x-consent-token",
"required": false,
"schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "Optional consent token",
"title": "X-Consent-Token",
"type": "string"
"title": "X-Consent-Token"
}
},
{
Expand All @@ -236,9 +267,16 @@
"name": "authorization",
"required": false,
"schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "The login token. Value should be \"Bearer [token]\"",
"title": "Authorization",
"type": "string"
"title": "Authorization"
}
},
{
Expand All @@ -247,9 +285,16 @@
"name": "x-authorization-provider",
"required": false,
"schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"description": "The bare domain of the system that provided the token.",
"title": "X-Authorization-Provider",
"type": "string"
"title": "X-Authorization-Provider"
}
}
],
Expand Down
Loading