OpenAPI document
OasMediaType
Represents a Media Type Object in the OpenAPI Specification.
The OasMediaType class encapsulates the definition of content for different media types in request bodies and responses. It defines the structure, format, and validation rules for content using schemas and provides examples for documentation and testing purposes.
This class supports various content types including JSON, XML, form data, file uploads, and binary content, making it essential for describing API request and response payloads.
Key Features
- Schema Validation: Define content structure through OpenAPI schemas
- Multiple Examples: Rich example support for documentation and testing
- Encoding Support: Handle multipart and form data encoding
- Content Type Support: JSON, XML, binary, form data, and more
- Reference Integration: Work seamlessly with schema references
Signature
import { OasMediaType } from '@skmtc/core'
class OasMediaType extends OasBaseExamples
import { OasMediaType, OasObject, OasString } from '@skmtc/core';
const jsonContent = new OasMediaType({
mediaType: 'application/json',
schema: new OasObject({
properties: {
name: new OasString(),
email: new OasString({ format: 'email' })
}
}),
examples: {
user: new OasExample({
summary: 'Sample user',
value: { name: 'John Doe', email: 'john@example.com' }
})
}
});const fileUpload = new OasMediaType({
mediaType: 'multipart/form-data',
schema: new OasObject({
properties: {
file: new OasString({ format: 'binary' }),
description: new OasString()
}
}),
encoding: {
file: {
contentType: 'image/png, image/jpeg',
style: 'form'
}
}
});const xmlContent = new OasMediaType({
mediaType: 'application/xml',
schema: new OasObject({
properties: {
product: new OasRef({ $ref: '#/components/schemas/Product' })
}
}),
examples: {
sample: new OasExample({
value: '<product><name>Widget</name><price>19.99</price></product>'
})
}
});Constructor
constructor
constructor(fields: MediaTypeFields, context?: ParseContextType)Creates a new OasMediaType instance.
Media type configuration including content schema, examples, and encoding
Properties
Type identifier for this OAS media type
The media type identifier (e.g., 'application/json', 'text/xml')
The schema defining the structure and validation rules for the content
The schema of each item in a sequential media type (OpenAPI 3.2)
Example values demonstrating the media type content
Custom extension fields (x-* properties) defined for this media type
Encoding information for multipart and form data serialization
Positional Encoding Objects for a multipart array (OpenAPI 3.2), stored as-is
Encoding Object for each array item (OpenAPI 3.2), stored as-is
Methods
toJsonSchema
toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.MediaTypeObjectConverts this OAS media type to an OpenAPI v3 JSON schema representation.
Conversion options including reference handling and formatting preferences
OpenAPI v3 media type object with schema and examples