Skip to content
Open
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
3 changes: 3 additions & 0 deletions e2e/src/cats/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export class CreateCatDto {
@ApiProperty({ description: 'tag', required: false })
readonly tag: TagDto;

@ApiProperty({ description: 'nullable tag', nullable: true, type: () => TagDto })
readonly nullableTag: TagDto;

nested: {
first: string;
second: number;
Expand Down
12 changes: 12 additions & 0 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ describe('Validate OpenAPI schema', () => {
});
});

it('should include type field when nullable is used with allOf (issue #3274)', () => {
const document = SwaggerModule.createDocument(app, options);
const createCatDtoSchema = document.components?.schemas
?.CreateCatDto as SchemaObject;
expect(createCatDtoSchema.properties.nullableTag).toEqual({
description: 'nullable tag',
nullable: true,
type: 'object',
allOf: [{ $ref: '#/components/schemas/TagDto' }]
});
});

it('should not add optional properties to required list', () => {
const document = SwaggerModule.createDocument(app, options);
const required = (document.components?.schemas?.Cat as SchemaObject)
Expand Down
3 changes: 2 additions & 1 deletion lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class SchemaObjectFactory {
schemaObjectMetadata.items[declaredSchemaCombinator] =
property[declaredSchemaCombinator];
delete property[declaredSchemaCombinator];
} else {
} else if (!schemaObjectMetadata['nullable']) {
delete schemaObjectMetadata.type;
}
}
Expand Down Expand Up @@ -525,6 +525,7 @@ export class SchemaObjectFactory {
name: metadata.name || key,
required: metadata.required,
...validMetadataObject,
...(validMetadataObject['nullable'] ? { type: 'object' } : {}),
allOf: [{ $ref }]
} as SchemaObjectMetadata;
}
Expand Down
30 changes: 30 additions & 0 deletions test/services/schema-object-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ describe('SchemaObjectFactory', () => {
profile: {
description: 'Profile',
nullable: true,
type: 'object',
allOf: [
{
$ref: '#/components/schemas/CreateProfileDto'
Expand Down Expand Up @@ -395,6 +396,35 @@ describe('SchemaObjectFactory', () => {
});
});

it('should include type "object" for nullable $ref properties (issue #3274)', () => {
class ProfileDto {
@ApiProperty()
bio: string;
}

class UserWithNullableProfile {
@ApiProperty({
nullable: true,
type: () => ProfileDto
})
profile: ProfileDto;
}

const schemas: Record<string, SchemasObject> = {};
schemaObjectFactory.exploreModelSchema(
UserWithNullableProfile,
schemas
);
expect(
(schemas['UserWithNullableProfile'] as Record<string, any>).properties
.profile
).toEqual({
nullable: true,
type: 'object',
allOf: [{ $ref: '#/components/schemas/ProfileDto' }]
});
});

it('should purge linked types from properties', () => {
class Human {
@ApiProperty()
Expand Down