Given a Python dataclass definition, the utility will produce a corresponding JSON Schema dictionary. For instance, consider these two Python data structures, where UserProfile includes a nested Address and various standard and custom types:
```python import dataclasses from enum import Enum from typing import Optional, List import datetime import uuid
class Status(Enum): ACTIVE = "active" INACTIVE = "inactive"
@dataclasses.dataclass class Address: street: str city: str zip_code: str
@dataclasses.dataclass class UserProfile: user_id: uuid.UUID username: str email: Optional[str] = None age: int = 30 is_active: bool = True roles: List[str] status: Status created_at: datetime.datetime billing_address: Address ```
Executing generate_json_schema(UserProfile) would yield the following JSON Schema. This output correctly translates nested models, enumerations, optional fields, and default values into their JSON Schema equivalents. Custom types like uuid.UUID and datetime.datetime are mapped to string with appropriate format attributes, ensuring broad compatibility for API specifications.
``json { "title": "UserProfile", "type": "object", "properties": { "user_id": { "type": "string", "format": "uuid" }, "username": { "type": "string" }, "email": { "type": "string" }, "age": { "type": "integer", "default": 30 }, "is_active": { "type": "boolean", "default": true }, "roles": { "type": "array", "items": { "type": "string" } }, "status": { "type": "string", "enum": ["active", "inactive"] }, "created_at": { "type": "string", "format": "date-time" }, "billing_address": { "$ref": "#/$defs/Address" } }, "required": [ "user_id", "username", "roles", "status", "created_at", "billing_address" ], "$defs": { "Address": { "title": "Address", "type": "object", "properties": { "street": { "type": "string" }, "city": { "type": "string" }, "zip_code": { "type": "string" } }, "required": ["street", "city", "zip_code"] } } } ``
This schema accurately reflects the Python type definitions. Non-optional fields without defaults are marked as required, Enum members are correctly represented as a string enum array, and nested dataclasses are handled with $ref and $defs for proper schema modularity. This structure is suitable for direct inclusion in OpenAPI specifications.