question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nullable function parameters in edmx produce non nullable TS types in generated files.

See original GitHub issue

Describe the bug Nullable function parameters in edmx produce non nullable TS types

To Reproduce

Given the following edmx and the 1.46.0 generator it produces

Usage

import { getMessages } from './api/gen/chat-service';
const response = await getMessages({ other: 'some-contact', id: null });

/*
 error TS2322: Type 'string | null' is not assignable to type 'string'.
  Type 'null' is not assignable to type 'string'.
*/

Generated

/*
 * Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved.
 *
 * This is a generated file powered by the SAP Cloud SDK for JavaScript.
 */
import { transformReturnValueForEntityListV4, FunctionImportRequestBuilderV4, FunctionImportParameter } from '@sap-cloud-sdk/core';
import { ChatMessages } from './ChatMessages';

/**
 * Type of the parameters to be passed to [[getMessages]].
 */
export interface GetMessagesParameters {
  /**
   * Other.
   */
  other: string;
  /**
   * Id.
   */
  id: string;
}

/**
 * Get Messages. 
 *
 * @param parameters - Object containing all parameters for the function import.
 * @returns A request builder that allows to overwrite some of the values and execute the resulting request.
 */
export function getMessages(parameters: GetMessagesParameters): FunctionImportRequestBuilderV4<GetMessagesParameters, ChatMessages[]> {
  const params = {
    other: new FunctionImportParameter('other', 'Edm.String', parameters.other),
    id: new FunctionImportParameter('id', 'Edm.Guid', parameters.id)
  }

  return new FunctionImportRequestBuilderV4('/chat', 'getMessages', (data) => transformReturnValueForEntityListV4(data, ChatMessages), params);
}

export const functionImports = {
  getMessages
};

EDMX

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
  <edmx:DataServices>
    <Schema Namespace="ChatService" xmlns="http://docs.oasis-open.org/odata/ns/edm">
      <EntityContainer Name="EntityContainer">
        <EntitySet Name="ChatMessages" EntityType="ChatService.ChatMessages" />
        <FunctionImport Name="getMessages" Function="ChatService.getMessages" EntitySet="ChatMessages"/>
      </EntityContainer>
      <EntityType Name="ChatMessages">
        <Key>
          <PropertyRef Name="ID"/>
        </Key>
        <Property Name="ID" Type="Edm.Guid" Nullable="false"/>
      </EntityType>
      <Function Name="getMessages" IsBound="false" IsComposable="false">
        <Parameter Name="other" Type="Edm.String" Nullable="false"/>
        <Parameter Name="id" Type="Edm.Guid"/>
        <ReturnType Type="Collection(ChatService.ChatMessages)" Nullable="false"/>
      </Function>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>
const generator = require('@sap-cloud-sdk/generator');
const inputDir = 'src/api/odata';
const outputDir = 'src/api/gen';

const generatorConfig = {
    forceOverwrite: true,
    generateJs: false,
    useSwagger: false,
    writeReadme: false,
    clearOutputDir: true,
    generateNpmrc: false,
    generateTypedocJson: false,
    generatePackageJson: false,
    generateCSN: false,
    sdkAfterVersionScript: false,
    s4hanaCloud: false,
};

generator.generate({
    ...generatorConfig,
    inputDir,
    outputDir,
});

service-mapping.json

{
  "chat": {
    "directoryName": "chat-service",
    "servicePath": "/chat",
    "npmPackageName": "chat-service"
  }
}

Expected behaviour I’d expect the type for id to be string | null. Any type of field seems to default not null, so it doesn’t matter I used UUID here.

Used Versions:

  • node v14.15.5
  • npm 6.14.11
  • SAP Cloud SDK version 1.46.0

Log file

[2021-07-14T08:39:20.723Z] INFO     (generator): [chat] Generating entities ...
[2021-07-14T08:39:20.732Z] INFO     (generator): [chat] Generating batch request builder ...
[2021-07-14T08:39:20.836Z] INFO     (generator): Generating entity: ChatMessages...
[2021-07-14T08:39:20.893Z] INFO     (generator): [chat] Generating function imports ...
[INFO] 11:39:21 ts-node-dev ver. 1.1.6 (using ts-node ver. 9.1.1, typescript ver. 4.2.3)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
marikanercommented, Aug 2, 2021

Hey @krodyrobi,

a fix to this issue was merged. We will release a new version with this fix soon. Feel free to try it with the canary version in the mean time.

0reactions
FrankEssenbergercommented, Jul 23, 2021

ok I found this also out in parallel. Then I will adjust the generation of the. client accordingly that the default is nullable if not stated otherwise.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does EF determine nullable parameters? - Stack Overflow
In case it helps, the process I use with EF is: Create SP; Update Model (edmx); Add new function import; Generate new complex...
Read more >
Working with nullable reference types - EF Core
Another strategy is to use non-nullable auto-properties, but to initialize them to null, using the null-forgiving operator (!) to silence the ...
Read more >
Preparing Entity Framework Core for Static Analysis and ...
For EF Core the solution is to map the non-nullable field to a nullable property. Theoretically this will only be null for a...
Read more >
Nullable Reference types in C# – Best practices | DotNetCurry
In this tutorial, I look at the state of the Nullable Reference Types feature in C#, one year after its initial release.
Read more >
Restrict null and undefined via Non-Nullable-Types in ...
Let's say you want to write a TypeScript function that takes a text as a parameter and returns a trimmed and lower-cased version...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found