Skip to content

Change a schema that is not used in any endpoint

In some situations, we may want to use versioning not just for our openapi schemas and endpoints but also within our code such as when we want to send versioned webhooks to our clients.

For example, let's say we want to change the type of an "id" field from integer to string:

from pydantic import BaseModel

from cadwyn import ResponseInfo, VersionChange, convert_response_to_previous_version_for, schema


# User from latest version
class User(BaseModel):
    id: str


class ChangeUserIDToString(VersionChange):
    description = (
        "Change users' ID field to a string to support any kind of ID. "
        "Be careful: if you use a non-integer ID in a new version and "
        "try to get it from the old version, the ID will be zero in response"
    )
    instructions_to_migrate_to_previous_version = [
        schema(User).field("id").had(type=int),
    ]

    @convert_response_to_previous_version_for(User)
    def change_id_to_int(response: ResponseInfo): ...

Unless there is an endpoint that has User as its response_model, this code will end up causing an error when we run our Cadwyn app. This is because Cadwyn tries to make sure that all of your converters apply to at least one endpoint. Otherwise, it would be too easy for you to make a mistake when writing converters for the wrong schemas.

To avoid it, set check_usage=False:

from pydantic import BaseModel

from cadwyn import ResponseInfo, VersionChange, convert_response_to_previous_version_for, schema


# User from latest version
class User(BaseModel):
    id: str


class ChangeUserIDToString(VersionChange):
    description = (
        "Change users' ID field to a string to support any kind of ID. "
        "Be careful: if you use a non-integer ID in a new version and "
        "try to get it from the old version, the ID will be zero in response"
    )
    instructions_to_migrate_to_previous_version = [
        schema(User).field("id").had(type=int),
    ]

    @convert_response_to_previous_version_for(User, check_usage=False)
    def change_id_to_int(response: ResponseInfo): ...