From bb7f3b4ba4584356fedd7e1ae448d4a47087c017 Mon Sep 17 00:00:00 2001 From: loup Date: Mon, 16 Nov 2026 14:08:53 +0100 Subject: [PATCH] fix(api): allow null runtime and casting order in responses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Loading a season's episodes returned 500. parseResponseDto validates the response against its DTO and only tolerates a null when @ApiProperty declares nullable: true, which runtime did not — while 2 786 364 of 6 913 342 rows in tmdb.tv_episode (40.3%) have runtime IS NULL, because TMDB does not publish a duration for every episode. Any season holding one such episode failed. The casting error has a different cause despite the same symptom. tmdb.tv_series_role.order is NOT NULL, so the nulls are produced by the query itself: getCasting left-joins credits to roles, and a credit with no role row yields MIN(role.order) = NULL and jsonb_agg over a row of nulls, i.e. a phantom role [{"character":null,"order":null}]. 556 910 of 3 205 162 Actor credits (17.4%) have no role row at all. So the two are fixed differently. runtime is genuinely nullable and gets @IsNullable. The phantom role is a bug and is removed at the source with FILTER (WHERE credit_id IS NOT NULL) and COALESCE to '[]', which keeps TvSeriesRoleDto.order and MovieRoleDto.order non-nullable — after the filter the array only ever holds real roles. Only the top-level casting order becomes nullable, since MIN over an empty group has no value to report. Verified with tsc --noEmit on apps/api and prettier --check. --- apps/api/src/app/movies/dto/movie-credits.dto.ts | 8 ++++++-- apps/api/src/app/movies/movies.service.ts | 13 ++++++++----- .../src/app/tv-series/dto/tv-series-credits.dto.ts | 8 ++++++-- .../tv-series/seasons/episodes/tv-episodes.dto.ts | 6 ++++-- apps/api/src/app/tv-series/tv-series.service.ts | 13 ++++++++----- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/apps/api/src/app/movies/dto/movie-credits.dto.ts b/apps/api/src/app/movies/dto/movie-credits.dto.ts index 1bf2184d..50a13028 100644 --- a/apps/api/src/app/movies/dto/movie-credits.dto.ts +++ b/apps/api/src/app/movies/dto/movie-credits.dto.ts @@ -30,10 +30,14 @@ export class MovieCastingDto { @IsInt() personId!: number; - @ApiProperty({ description: 'The primary ordering index for this actor' }) + @ApiProperty({ + description: 'The primary ordering index for this actor', + nullable: true, + }) @Expose() + @IsNullable() @IsInt() - order!: number; + order!: number | null; @ApiProperty({ type: () => PersonCompactDto }) @Expose() diff --git a/apps/api/src/app/movies/movies.service.ts b/apps/api/src/app/movies/movies.service.ts index 4d507e13..7d565406 100644 --- a/apps/api/src/app/movies/movies.service.ts +++ b/apps/api/src/app/movies/movies.service.ts @@ -57,11 +57,14 @@ export class MoviesService { personId: tmdbMovieCredit.personId, order: sql`MIN(${tmdbMovieRole.order})`.as('min_order'), roles: sql[]>` - jsonb_agg( - jsonb_build_object( - 'character', ${tmdbMovieRole.character}, - 'order', ${tmdbMovieRole.order} - ) ORDER BY ${tmdbMovieRole.order} ASC + COALESCE( + jsonb_agg( + jsonb_build_object( + 'character', ${tmdbMovieRole.character}, + 'order', ${tmdbMovieRole.order} + ) ORDER BY ${tmdbMovieRole.order} ASC + ) FILTER (WHERE ${tmdbMovieRole.creditId} IS NOT NULL), + '[]'::jsonb ) `.as('roles_agg'), }) diff --git a/apps/api/src/app/tv-series/dto/tv-series-credits.dto.ts b/apps/api/src/app/tv-series/dto/tv-series-credits.dto.ts index 53371f3a..6790959d 100644 --- a/apps/api/src/app/tv-series/dto/tv-series-credits.dto.ts +++ b/apps/api/src/app/tv-series/dto/tv-series-credits.dto.ts @@ -30,10 +30,14 @@ export class TvSeriesCastingDto { @IsInt() personId!: number; - @ApiProperty({ description: 'The primary ordering index for this actor' }) + @ApiProperty({ + description: 'The primary ordering index for this actor', + nullable: true, + }) @Expose() + @IsNullable() @IsInt() - order!: number; + order!: number | null; @ApiProperty({ type: () => PersonCompactDto }) @Expose() diff --git a/apps/api/src/app/tv-series/seasons/episodes/tv-episodes.dto.ts b/apps/api/src/app/tv-series/seasons/episodes/tv-episodes.dto.ts index 6c0e6137..460d6308 100644 --- a/apps/api/src/app/tv-series/seasons/episodes/tv-episodes.dto.ts +++ b/apps/api/src/app/tv-series/seasons/episodes/tv-episodes.dto.ts @@ -8,6 +8,7 @@ import { IsString, ValidateNested, } from 'class-validator'; +import { IsNullable } from '../../../../common/decorators/is-nullable.decorator'; import { Expose, Transform, Type } from 'class-transformer'; import { PaginatedResponseDto, PaginationQueryDto } from '../../../../common/dto/pagination.dto'; import { SortOrder } from '../../../../common/dto/sort.dto'; @@ -84,10 +85,11 @@ export class TvEpisodeDto { @IsString() overview!: string | null; - @ApiProperty({ example: 60 }) + @ApiProperty({ example: 60, nullable: true }) @Expose() + @IsNullable() @IsInt() - runtime!: number; + runtime!: number | null; @ApiProperty({ example: '101', nullable: true }) @Expose() diff --git a/apps/api/src/app/tv-series/tv-series.service.ts b/apps/api/src/app/tv-series/tv-series.service.ts index 80212fc2..2a35cb1e 100644 --- a/apps/api/src/app/tv-series/tv-series.service.ts +++ b/apps/api/src/app/tv-series/tv-series.service.ts @@ -97,11 +97,14 @@ export class TvSeriesService { personId: tmdbTvSeriesCredit.personId, order: sql`MIN(${tmdbTvSeriesRole.order})`.as('min_order'), roles: sql[]>` - jsonb_agg( - jsonb_build_object( - 'character', ${tmdbTvSeriesRole.character}, - 'order', ${tmdbTvSeriesRole.order} - ) ORDER BY ${tmdbTvSeriesRole.order} ASC + COALESCE( + jsonb_agg( + jsonb_build_object( + 'character', ${tmdbTvSeriesRole.character}, + 'order', ${tmdbTvSeriesRole.order} + ) ORDER BY ${tmdbTvSeriesRole.order} ASC + ) FILTER (WHERE ${tmdbTvSeriesRole.creditId} IS NOT NULL), + '[]'::jsonb ) `.as('roles_agg'), })