Skip to main content

Typed Additional Model Properties Support in All SDKs

· 4 min read

We’re introducing support for typed additional model properties across all SDKs, enhancing type safety and alignment with OpenAPI specifications.

Details

Model classes in C#, Java, PHP, Python, Ruby, Go, and TypeScript SDKs now support typed additional properties. This update improves type safety and aligns with OpenAPI specifications by allowing models to store additional properties with specific, user-defined data types. The feature enhances data consistency, reduces runtime errors, and improves SDK flexibility while closely adhering to OpenAPI standards.

Open Api Example

Here’s an example schema of type StudentResult, which contains an email field for student identification and additional properties of type number. The StudentResult model represents a student's results in various subjects, where the subject names are the keys and their respective scores are the values:

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

What's New?

Previously, the EnableAdditionalModelProperties CodeGen setting allowed additional properties in model classes to be stored using a generic any type, leaving type management to the user. With this update, SDKs now support user-defined types for additional properties, ensuring more reliable data handling and seamless integration with OpenAPI specifications.

With this release, enabling ExtendedAdditionalPropertiesSupport through CodeGen Settings will automatically provide typed additional properties in your SDK models.

Key Features

  • Typed Additional Properties: Users can now define specific types for additionalProperties, enhancing type safety.
  • Inheritance Handling: In cases of typed additionalProperties definitions in inherited models, the root model's definition will apply to all child models, regardless of whether it is disabled in individual child models. This minimizes type conflicts and ensures consistency.
  • Conflict Prevention: Adding additionalProperties with keys that conflict with existing model properties will trigger runtime exceptions, catching issues early during serialization/deserialization.

SDK Request Examples

Here’s how to define a request object of type StudentResult in various languages:

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 properties
"Functional programming": 78.3
}

After deserialization, the "Continuous mathematics" additional property will be ignored due to invalid type. To access "Functional programming" additional property use the following code snippet to print the value in different programming languages:

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