Skip to content

refactor: fix null type errors#805

Open
Synar wants to merge 5 commits intomainfrom
ali/fix-null-type-errors-1
Open

refactor: fix null type errors#805
Synar wants to merge 5 commits intomainfrom
ali/fix-null-type-errors-1

Conversation

@Synar
Copy link
Copy Markdown
Contributor

@Synar Synar commented Feb 4, 2026

Properly handle null and undefined values in many functions without lying on the types, and without ignoring the possibility of such values which can lead to crashes (such as OpenRailAssociation/osrd#14085 which this pr fixes)

There are a few arbitrary decisions taken in this pr which can be debated. In particular, how much we want to throw or use "!" VS how much we want to fallback or use "?". I generally tried to assume that getters could be called before init and so avoided as much as possible to fail on null values for these, but for a few setters I was more lax. The fallback values when sorting are a bit ugly too as a workaround.

The pr can be checked by setting "strictNullChecks": true in tsconfig.json. All edited files should have 0 errors after this pr, except node.service which is only partially handled by this pr.

Synar added 5 commits February 4, 2026 23:12
Signed-off-by: Alice K. <alice.khoudli@gmail.com>
Signed-off-by: Alice K. <alice.khoudli@gmail.com>
Signed-off-by: Alice K. <alice.khoudli@gmail.com>
Signed-off-by: Alice K. <alice.khoudli@gmail.com>
Signed-off-by: Alice K. <alice.khoudli@gmail.com>
@Synar Synar self-assigned this Feb 4, 2026
@Synar Synar requested a review from aiAdrian as a code owner February 4, 2026 22:28
@Synar Synar added this to Board PI 18 Feb 4, 2026
@Synar Synar moved this to Awaiting merge in Board PI 18 Feb 4, 2026
@emersion emersion self-requested a review February 11, 2026 09:12
@Synar Synar moved this to Awaiting merge in Board PI 19 Feb 14, 2026
@Synar Synar removed this from Board PI 19 Mar 5, 2026
@Synar Synar requested review from Caracol3 and Math-R March 5, 2026 16:31
@louisgreiner louisgreiner requested a review from Pivouane March 10, 2026 10:08
Copy link
Copy Markdown
Contributor

@louisgreiner louisgreiner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

The commit messages need a slight overwork, we use Conventional Commit, then this should be "refactor", not "refacto":

["feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore"],

Copy link
Copy Markdown
Member

@emersion emersion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on the sortPorts() cleanup!

Can the cleaned up files be added to tsconfig.strictNullChecks.json, to ensure they don't regress?

connections: ConnectionDto[]; // all connections aligned to the node

resourceId: number; // reference to the algined (resource - not yet implemented)
resourceId: number | null; // reference to the algined (resource - not yet implemented)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use | undefined here instead of | null? For DTO specifically, it better reflects that old JSON files are missing this field.

}

getPort(portId: number): Port {
getPort(portId: number | undefined): Port | undefined {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds a bit weird to accept undefined here - any reason why we need to?


getNextTrainrunSection(trainrunSection: TrainrunSection): TrainrunSection {
getNextTrainrunSection(
trainrunSection: TrainrunSection | undefined | null,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

});
if (transition !== undefined) {
return this.getPort(transition.getPortId1()).getTrainrunSection();
return this.getPort(transition.getPortId1())?.getTrainrunSection() ?? undefined;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't getPort() return undefined already?

): TrainrunSection | undefined {
const portsForTrainrun = this.ports.filter(
(port) => port.getTrainrunSection().getTrainrunId() === trainrunId,
(port) => port?.getTrainrunSection()?.getTrainrunId() === trainrunId,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

port should never be undefined here.

): TrainrunSection | undefined {
const portsForTrainrun = this.ports.filter(
(port) => port.getTrainrunSection().getTrainrunId() === trainrunId,
(port) => port?.getTrainrunSection()?.getTrainrunId() === trainrunId,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds weird to have a port not be attached to any trainrun section. Can this really happen?

If it's not supposed to happen, can we null-assert or throw an error in getTrainrunSection() instead?

mergeConnections(
netzgrafikDto: NetzgrafikDto,
trainrunSectionMap: Map<number, number>,
trainrunSectionMap: Map<number | undefined, number>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which situation does it make sense to have undefined as the Map key?

(Wouldn't that result in key collisions anyways?)

}

getNodeFromId(nodeId: number): Node {
getNodeFromId(nodeId: number | undefined): Node | undefined {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too, I don't think we should accept an undefined ID.

@Math-R Math-R removed their request for review March 31, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Awaiting merge

Development

Successfully merging this pull request may close these issues.

3 participants