Skip to content

Fix/allof additional properties false #2287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-swans-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(zod): improve handling of additional properties
2 changes: 1 addition & 1 deletion README.md
7 changes: 7 additions & 0 deletions packages/openapi-ts-tests/main/test/2.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe(`OpenAPI ${version}`, () => {
};

const scenarios = [
{
config: createConfig({
input: 'additional-properties-false.json',
output: 'additional-properties-false',
}),
description: 'forbids arbitrary properties on objects',
},
{
config: createConfig({
input: 'additional-properties-true.json',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is auto-generated by @hey-api/openapi-ts

export type Foo = {
foo: string;
};

export type Bar = Foo & {};

export type Baz = Foo & {
bar: string;
};

export type ClientOptions = {
baseUrl: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export type Foo = {
foo: string;
};

export type Bar = Foo & {
[key: string]: never;
};
export type Bar = Foo & {};

export type Baz = Foo & {
bar: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export type Foo = {
foo: string;
};

export type Bar = Foo & {
[key: string]: never;
};
export type Bar = Foo & {};

export type Baz = Foo & {
bar: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"swagger": "2.0",
"info": {
"title": "OpenAPI 2.0 additional properties false example",
"version": "1"
},
"definitions": {
"Foo": {
"required": ["foo"],
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": false
},
"Bar": {
"allOf": [
{
"$ref": "#/definitions/Foo"
},
{
"type": "object",
"additionalProperties": false
}
]
},
"Baz": {
"allOf": [
{
"$ref": "#/definitions/Foo"
},
{
"required": ["bar"],
"type": "object",
"properties": {
"bar": {
"type": "string"
}
},
"additionalProperties": false
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is auto-generated by @hey-api/openapi-ts

export type Foo = {
foo: string;
};

export type Bar = Foo & {};

export type Baz = Foo & {
bar: string;
};

export type ClientOptions = {
baseUrl: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ export const zArrayWithProperties = z.array(z.object({
/**
* This is a string dictionary
*/
export const zDictionaryWithString = z.object({});
export const zDictionaryWithString = z.record(z.string(), z.string());

/**
* This is a string reference
*/
export const zDictionaryWithReference = z.object({});
export const zDictionaryWithReference = z.record(z.string(), zModelWithString);

/**
* This is a complex dictionary
*/
export const zDictionaryWithArray = z.object({});
export const zDictionaryWithArray = z.record(z.string(), z.array(zModelWithString));

/**
* This is a string dictionary
*/
export const zDictionaryWithDictionary = z.record(z.string(), z.object({}));
export const zDictionaryWithDictionary = z.record(z.string(), z.record(z.string(), z.string()));

/**
* This is a complex dictionary
Expand Down Expand Up @@ -239,8 +239,12 @@ export const zModelWithEnumFromDescription = z.object({
* This is a model with nested enums
*/
export const zModelWithNestedEnums = z.object({
dictionaryWithEnum: z.optional(z.object({})),
dictionaryWithEnumFromDescription: z.optional(z.object({})),
dictionaryWithEnum: z.optional(z.record(z.string(), z.enum([
'Success',
'Warning',
'Error'
]))),
dictionaryWithEnumFromDescription: z.optional(z.record(z.string(), z.int())),
arrayWithEnum: z.optional(z.array(z.enum([
'Success',
'Warning',
Expand Down Expand Up @@ -286,7 +290,7 @@ export const zModelWithArray = z.object({
* This is a model with one property containing a dictionary
*/
export const zModelWithDictionary = z.object({
prop: z.optional(z.object({}))
prop: z.optional(z.record(z.string(), z.string()))
});

/**
Expand Down Expand Up @@ -666,7 +670,7 @@ export const zTypesData = z.object({
parameterString: z._default(z.string(), 'default'),
parameterBoolean: z._default(z.boolean(), true),
parameterArray: z.array(z.string()),
parameterDictionary: z.object({}),
parameterDictionary: z.record(z.string(), z.unknown()),
parameterEnum: z.enum([
'Success',
'Warning',
Expand All @@ -679,7 +683,7 @@ export const zTypesResponse = z.union([
z.number(),
z.string(),
z.boolean(),
z.object({})
z.record(z.string(), z.unknown())
]);

export const zComplexTypesData = z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ export const zArrayWithProperties = z.array(z.object({
/**
* This is a string dictionary
*/
export const zDictionaryWithString = z.object({});
export const zDictionaryWithString = z.record(z.string());

/**
* This is a string reference
*/
export const zDictionaryWithReference = z.object({});
export const zDictionaryWithReference = z.record(zModelWithString);

/**
* This is a complex dictionary
*/
export const zDictionaryWithArray = z.object({});
export const zDictionaryWithArray = z.record(z.array(zModelWithString));

/**
* This is a string dictionary
*/
export const zDictionaryWithDictionary = z.record(z.object({}));
export const zDictionaryWithDictionary = z.record(z.record(z.string()));

/**
* This is a complex dictionary
Expand Down Expand Up @@ -239,8 +239,12 @@ export const zModelWithEnumFromDescription = z.object({
* This is a model with nested enums
*/
export const zModelWithNestedEnums = z.object({
dictionaryWithEnum: z.object({}).optional(),
dictionaryWithEnumFromDescription: z.object({}).optional(),
dictionaryWithEnum: z.record(z.enum([
'Success',
'Warning',
'Error'
])).optional(),
dictionaryWithEnumFromDescription: z.record(z.number().int()).optional(),
arrayWithEnum: z.array(z.enum([
'Success',
'Warning',
Expand Down Expand Up @@ -286,7 +290,7 @@ export const zModelWithArray = z.object({
* This is a model with one property containing a dictionary
*/
export const zModelWithDictionary = z.object({
prop: z.object({}).optional()
prop: z.record(z.string()).optional()
});

/**
Expand Down Expand Up @@ -664,7 +668,7 @@ export const zTypesData = z.object({
parameterString: z.string().default('default'),
parameterBoolean: z.boolean().default(true),
parameterArray: z.array(z.string()),
parameterDictionary: z.object({}),
parameterDictionary: z.record(z.unknown()),
parameterEnum: z.enum([
'Success',
'Warning',
Expand All @@ -677,7 +681,7 @@ export const zTypesResponse = z.union([
z.number(),
z.string(),
z.boolean(),
z.object({})
z.record(z.unknown())
]);

export const zComplexTypesData = z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ export const zArrayWithProperties = z.array(z.object({
/**
* This is a string dictionary
*/
export const zDictionaryWithString = z.object({});
export const zDictionaryWithString = z.record(z.string(), z.string());

/**
* This is a string reference
*/
export const zDictionaryWithReference = z.object({});
export const zDictionaryWithReference = z.record(z.string(), zModelWithString);

/**
* This is a complex dictionary
*/
export const zDictionaryWithArray = z.object({});
export const zDictionaryWithArray = z.record(z.string(), z.array(zModelWithString));

/**
* This is a string dictionary
*/
export const zDictionaryWithDictionary = z.record(z.string(), z.object({}));
export const zDictionaryWithDictionary = z.record(z.string(), z.record(z.string(), z.string()));

/**
* This is a complex dictionary
Expand Down Expand Up @@ -239,8 +239,12 @@ export const zModelWithEnumFromDescription = z.object({
* This is a model with nested enums
*/
export const zModelWithNestedEnums = z.object({
dictionaryWithEnum: z.optional(z.object({})),
dictionaryWithEnumFromDescription: z.optional(z.object({})),
dictionaryWithEnum: z.optional(z.record(z.string(), z.enum([
'Success',
'Warning',
'Error'
]))),
dictionaryWithEnumFromDescription: z.optional(z.record(z.string(), z.int())),
arrayWithEnum: z.optional(z.array(z.enum([
'Success',
'Warning',
Expand Down Expand Up @@ -286,7 +290,7 @@ export const zModelWithArray = z.object({
* This is a model with one property containing a dictionary
*/
export const zModelWithDictionary = z.object({
prop: z.optional(z.object({}))
prop: z.optional(z.record(z.string(), z.string()))
});

/**
Expand Down Expand Up @@ -666,7 +670,7 @@ export const zTypesData = z.object({
parameterString: z.string().default('default'),
parameterBoolean: z.boolean().default(true),
parameterArray: z.array(z.string()),
parameterDictionary: z.object({}),
parameterDictionary: z.record(z.string(), z.unknown()),
parameterEnum: z.enum([
'Success',
'Warning',
Expand All @@ -679,7 +683,7 @@ export const zTypesResponse = z.union([
z.number(),
z.string(),
z.boolean(),
z.object({})
z.record(z.string(), z.unknown())
]);

export const zComplexTypesData = z.object({
Expand Down
Loading
Loading