allOf (Composition)
Soxom flattens allOf schemas into composite types, preserving model references for inheritance patterns.
Basic Usage
Section titled “Basic Usage”OpenAPI Definition
Section titled “OpenAPI Definition”components: schemas: # Base schema with common fields TimestampedResource: type: object properties: created_at: type: string format: date-time readOnly: true updated_at: type: string format: date-time readOnly: true
# Composed schema using allOf User: allOf: - $ref: '#/components/schemas/TimestampedResource' - type: object required: - id - email - name properties: id: type: string format: uuid email: type: string format: email name: type: string status: type: string enum: [active, inactive]Generated Types
Section titled “Generated Types”// Base interfaceinterface TimestampedResource { readonly createdAt: Date; readonly updatedAt: Date;}
// Composed interface extends baseinterface User extends TimestampedResource { id: string; email: string; name: string; status?: 'active' | 'inactive';}# Base class@dataclassclass TimestampedResource: created_at: datetime updated_at: datetime
# Composed class inherits from base@dataclassclass User(TimestampedResource): id: str email: str name: str status: Optional[Literal["active", "inactive"]] = None// Base structtype TimestampedResource struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"`}
// Composed struct embeds basetype User struct { TimestampedResource ID string `json:"id"` Email string `json:"email"` Name string `json:"name"` Status string `json:"status,omitempty"`}// Base classpublic class TimestampedResource { private OffsetDateTime createdAt; private OffsetDateTime updatedAt; // getters...}
// Composed class extends basepublic class User extends TimestampedResource { private String id; private String email; private String name; private UserStatus status; // getters...}Flattening Behavior
Section titled “Flattening Behavior”Soxom handles allOf differently based on its contents:
Referenced Schemas ($ref)
Section titled “Referenced Schemas ($ref)”When allOf includes $ref, Soxom uses inheritance/composition:
User: allOf: - $ref: '#/components/schemas/BaseModel' # → extends/embeds - $ref: '#/components/schemas/Auditable' # → extends/embeds - type: object properties: # → own properties name: type: stringInline Schemas
Section titled “Inline Schemas”Inline object schemas are flattened into the parent:
User: allOf: - type: object # Flattened properties: id: type: string - type: object # Flattened properties: name: type: stringResults in:
interface User { id: string; name: string;}Inheritance Patterns
Section titled “Inheritance Patterns”Single Inheritance
Section titled “Single Inheritance”components: schemas: Animal: type: object properties: name: type: string age: type: integer
Dog: allOf: - $ref: '#/components/schemas/Animal' - type: object properties: breed: type: stringinterface Animal { name: string; age: number;}
interface Dog extends Animal { breed: string;}@dataclassclass Animal: name: str age: int
@dataclassclass Dog(Animal): breed: strMultiple Inheritance (Mixins)
Section titled “Multiple Inheritance (Mixins)”components: schemas: Timestamped: type: object properties: created_at: type: string format: date-time
Identifiable: type: object properties: id: type: string format: uuid
User: allOf: - $ref: '#/components/schemas/Timestamped' - $ref: '#/components/schemas/Identifiable' - type: object properties: email: type: stringinterface Timestamped { createdAt: Date;}
interface Identifiable { id: string;}
interface User extends Timestamped, Identifiable { email: string;}@dataclassclass Timestamped: created_at: datetime
@dataclassclass Identifiable: id: str
@dataclassclass User(Timestamped, Identifiable): email: strtype Timestamped struct { CreatedAt time.Time `json:"created_at"`}
type Identifiable struct { ID string `json:"id"`}
type User struct { Timestamped Identifiable Email string `json:"email"`}Property Overrides
Section titled “Property Overrides”When the same property appears multiple times, the last definition wins:
Base: type: object properties: status: type: string
Extended: allOf: - $ref: '#/components/schemas/Base' - type: object properties: status: type: string enum: [active, inactive] # More specificResult uses the more specific enum type.
Required Properties
Section titled “Required Properties”Required properties are merged from all schemas:
Base: type: object required: - id properties: id: type: string
User: allOf: - $ref: '#/components/schemas/Base' - type: object required: - email properties: email: type: stringUser has both id and email as required.
Best Practices
Section titled “Best Practices”- Use $ref for shared schemas - Enables proper inheritance
- Keep base schemas small - Single responsibility
- Avoid deep inheritance - 2-3 levels maximum
- Document compositions - Use descriptions
- Prefer mixins over deep hierarchies
Common Patterns
Section titled “Common Patterns”API Response Wrapper
Section titled “API Response Wrapper”ApiResponse: type: object properties: success: type: boolean timestamp: type: string format: date-time
UserResponse: allOf: - $ref: '#/components/schemas/ApiResponse' - type: object properties: data: $ref: '#/components/schemas/User'Create/Update Variants
Section titled “Create/Update Variants”UserBase: type: object properties: email: type: string name: type: string
UserCreate: allOf: - $ref: '#/components/schemas/UserBase' - type: object required: - email - name - password properties: password: type: string
UserUpdate: allOf: - $ref: '#/components/schemas/UserBase' # All fields optional for updates