Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
95 changes: 90 additions & 5 deletions okta/models/user_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
import json
import pprint
import re # noqa: F401
from datetime import datetime
from typing import Any, ClassVar, Dict, List
from typing import Optional, Set

from pydantic import BaseModel, ConfigDict, Field, StrictStr
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictStr
from typing_extensions import Self

from okta.models.user_type_links import UserTypeLinks


class UserType(BaseModel):
"""
Expand All @@ -40,8 +43,58 @@ class UserType(BaseModel):
replace of an existing user (but not a partial update).
""" # noqa: E501

id: Optional[StrictStr] = Field(default=None, description="The ID of the user type")
__properties: ClassVar[List[str]] = ["id"]
created: Optional[datetime] = Field(
default=None, description="A timestamp from when the user type was created"
)
created_by: Optional[StrictStr] = Field(
default=None,
description="The user ID of the account that created the user type",
alias="createdBy",
)
default: Optional[StrictBool] = Field(
default=None,
description="A boolean value to indicate if this is the default user type",
)
description: Optional[StrictStr] = Field(
default=None, description="The human-readable description of the user type"
)
display_name: Optional[StrictStr] = Field(
default=None,
description="The human-readable name of the user type",
alias="displayName",
)
id: Optional[StrictStr] = Field(
default=None, description="The unique key for the user type"
)
last_updated: Optional[datetime] = Field(
default=None,
description="A timestamp from when the user type was most recently updated",
alias="lastUpdated",
)
last_updated_by: Optional[StrictStr] = Field(
default=None,
description="The user ID of the most recent account to edit the user type",
alias="lastUpdatedBy",
)
name: Optional[StrictStr] = Field(
default=None,
description="The name of the user type. The name must start with A-Z or a-z "
"and contain only A-Z, a-z, 0-9, or underscore (_) characters. This value "
"becomes read-only after creation and can't be updated.",
)
links: Optional[UserTypeLinks] = Field(default=None, alias="_links")
__properties: ClassVar[List[str]] = [
"created",
"createdBy",
"default",
"description",
"displayName",
"id",
"lastUpdated",
"lastUpdatedBy",
"name",
"_links",
]

model_config = ConfigDict(
populate_by_name=True,
Expand Down Expand Up @@ -73,13 +126,28 @@ def to_dict(self) -> Dict[str, Any]:
were set at model initialization. Other fields with value `None`
are ignored.
"""
excluded_fields: Set[str] = set([])
excluded_fields: Set[str] = set(
[
"created",
"created_by",
"default",
"last_updated",
"last_updated_by",
]
)

_dict = self.model_dump(
by_alias=True,
exclude=excluded_fields,
exclude_none=True,
)
# override the default output from pydantic by calling `to_dict()` of links
if self.links:
if not isinstance(self.links, dict):
_dict["_links"] = self.links.to_dict()
else:
_dict["_links"] = self.links

return _dict

@classmethod
Expand All @@ -91,5 +159,22 @@ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
if not isinstance(obj, dict):
return cls.model_validate(obj)

_obj = cls.model_validate({"id": obj.get("id")})
_obj = cls.model_validate(
{
"created": obj.get("created"),
"createdBy": obj.get("createdBy"),
"default": obj.get("default"),
"description": obj.get("description"),
"displayName": obj.get("displayName"),
"id": obj.get("id"),
"lastUpdated": obj.get("lastUpdated"),
"lastUpdatedBy": obj.get("lastUpdatedBy"),
"name": obj.get("name"),
"_links": (
UserTypeLinks.from_dict(obj["_links"])
if obj.get("_links") is not None
else None
),
}
)
return _obj
43 changes: 43 additions & 0 deletions tests/test_user_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from okta.models.user_type import UserType


def test_user_type_from_dict_preserves_response_fields():
user_type = UserType.from_dict(
{
"id": "oty123",
"name": "employee",
"displayName": "Employee",
"description": "Employee user type",
"default": False,
"createdBy": "00ucreated",
"created": "2026-01-02T03:04:05.000Z",
"lastUpdatedBy": "00uupdated",
"lastUpdated": "2026-01-03T04:05:06.000Z",
}
)

assert user_type.id == "oty123"
assert user_type.name == "employee"
assert user_type.display_name == "Employee"
assert user_type.description == "Employee user type"
assert user_type.default is False
assert user_type.created_by == "00ucreated"
assert user_type.created is not None
assert user_type.last_updated_by == "00uupdated"
assert user_type.last_updated is not None


def test_user_type_to_dict_keeps_create_and_update_fields():
user_type = UserType(
id="oty123",
name="employee",
displayName="Employee",
description="Employee user type",
)

assert user_type.to_dict() == {
"description": "Employee user type",
"displayName": "Employee",
"id": "oty123",
"name": "employee",
}