Skip to content
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

[BUG] Unquoted enum values in unmanaged associations #446

Open
1 task done
MarvinWeitz opened this issue Dec 19, 2024 · 3 comments
Open
1 task done

[BUG] Unquoted enum values in unmanaged associations #446

MarvinWeitz opened this issue Dec 19, 2024 · 3 comments
Assignees
Labels
bug Something isn't working keepalive Will not be closed by Stale bot new

Comments

@MarvinWeitz
Copy link

MarvinWeitz commented Dec 19, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Nature of Your Project

TypeScript

Current Behavior

When using an unmanaged association to a String enum, the enum values are not quoted in the generated types. This causes Cannot find name errors:

entity Books {
  key ID              : Integer;
      title           : String;
      stock           : Integer;
      quality         : Association to Qualities
                          on quality.quality = quality_quality;
      quality_quality : type of Qualities : quality;
}

entity Qualities {
  key quality : String enum {
        good = 'G';
        bad  = 'B';
      };
}
...

// enum
const Book_quality_quality = {
  good: G, // error
  bad: B, // error
} as const;
type Book_quality_quality = G | B // error

...

Expected Behavior

It would be expected to have the strings quoted:

...

// enum
const Book_quality_quality = {
  good: "G",
  bad: "B",
} as const;
type Book_quality_quality = "G" | "B"

...

Steps To Reproduce

  1. cds init bookshop --add typescript,typer
  2. npm i
  3. Save cds fiel to generate types

Environment

- **OS**: SAP BAS
- **Node**: v20.12.0
- **npm**: 10.5.0
- **cds-typer**: 0.31.0
- **cds**: 8.6.0

Repository Containing a Minimal Reproducible Example

https://github.com/MarvinWeitz/cds-typer-quote-bug

Anything else?

No response

@MarvinWeitz MarvinWeitz added bug Something isn't working new labels Dec 19, 2024
@MarvinWeitz
Copy link
Author

Maybe it helps with a fix: The problem occurs because of enumVal in ./lib/components/enum.js. With the minimal example I've provided, the enumType is an array ['my.bookshop.Qualities', 'quality'] so the value is not escaped anymore.

@daogrady daogrady self-assigned this Dec 19, 2024
@daogrady
Copy link
Contributor

Hi Marvin,

thanks for reporting this bug. I will look into it, but it will probably not happen before 2025. 🎄

Best,
Daniel

@daogrady daogrady added the keepalive Will not be closed by Stale bot label Dec 19, 2024
@MarvinWeitz
Copy link
Author

No worries, have some nice holidays!

For anyone coming across this, I've quick-fixed this locally with a script that changes the "faulty" line of code to escape any value, independent of enumType.

const fs = require('fs');
const path = require('path');

// Path to the target file
const filePath = path.resolve(__dirname, './node_modules/@cap-js/cds-typer/lib/components/enum.js');

// Read the file
fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading file:', err);
        return;
    }

    // Replace the specific line of code
    const updatedData = data.replace(
        /enumType === 'cds\.String' \? JSON\.stringify\(`\$\{value \?\? key\}`\) : value/g,
        'JSON.stringify(`${value ?? key}`)'
    );

    // Check if a change was made
    if (updatedData !== data) {
        // Write the updated content back to the file
        fs.writeFile(filePath, updatedData, 'utf8', (writeErr) => {
            if (writeErr) {
                console.error('Error writing to file:', writeErr);
            } else {
                console.log('File successfully updated.');
            }
        });
    } else {
        console.log('No changes were made, pattern not found.');
    }
});

that I call in the postinstall script of the package.json:

{
  "scripts": {
    "postinstall": "node cds-typer-bugfix.js"
  }
}

PLEASE NOTE: This works in my case. As I don't know for what enumVal is used for, it's not guaranteed to not break stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working keepalive Will not be closed by Stale bot new
Projects
None yet
Development

No branches or pull requests

2 participants