Skip to main content

Additional Model Properties

APIMatic SDKs provide support for the JSON Schema additionalProperties keyword, which controls how additional properties(those not defined in properties or patternProperties) are handled. This enables SDKs to handle dynamic or unpredictable data structures in API requests and responses.

Configure Additional Model Properties

To leverage additional properties in your SDKs, enable the ExtendedAdditionalPropertiesSupport Code Generation setting while generating your SDKs.

Additional properties can be defined in OpenAPI definitions using the additionalProperties keyword, as demonstrated below:

components:
schemas:
StudentResult:
type: object
required:
- email
properties:
email:
type: string
format: email
additionalProperties:
type: number

In this example, the StudentResult model defines a required email property of type string. Additionally, it supports any number of additionalProperties where keys are strings, and values are number.

Usage in SDK

After configuring your API definition, the generated SDK will expose these additional properties with type safety. For instance:

SDK Request Examples

Here’s how an object of type StudentResult can be initialized:

const body: StudentResult = {
email: 'student616@oxford.ac.uk',
additionalProperties: {
'Theory Of Automata': 82.1,
'Computational complexity': 72.5,
'Functional programming': 78.3
},
};

SDK Response Examples

Here’s a sample JSON response representing a StudentResult object:

{
"email": "student616@oxford.ac.uk",
"Theory Of Automata": 82.1,
"Computational complexity": 72.5,
"Continuous mathematics": "87", // invalid additional property
"Functional programming": 78.3
}

During deserialization, the "Continuous mathematics" property will be ignored due to a type mismatch, while the "Functional programming" property will be correctly deserialized. The "Functional programming" property can be printed to the console as follows:

// Printing the value of `Functional programming` AdditionalProperty
console.log(response.result.additionalProperties['Functional programming']);
// Output: 78.3

Real-World Use Cases

Flexible Data Structures: Handle unknown or optional fields returned in API responses, such as metadata or custom attributes.

Extensible Models: Support user-defined properties in API requests while ensuring consistency and type correctness.