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(cli): type is missing when using type alias for string #4211

Closed
derdeka opened this issue Nov 26, 2019 · 5 comments
Closed

bug(cli): type is missing when using type alias for string #4211

derdeka opened this issue Nov 26, 2019 · 5 comments
Labels
bug CLI Relations Model relations (has many, etc.) stale

Comments

@derdeka
Copy link
Contributor

derdeka commented Nov 26, 2019

Steps to reproduce

I've defined a type alias export type UUID = string; to be used whenever a UUID is used. This helps the developers to easily read the application code and understand which type of string is expected there.

Id's in Models are defined this way:

@property({
  id: true,
  generated: true,
  jsonSchema: {
    pattern: PATTERN_UUID,
  },
})
id?: UUID;

When creating controller code by lb4 relation, the type of the ID is not correctly resolved.

Current Behavior

When using lb4 relation the cli does not correctly resolve the type of the ID, something like this is generated:

image

Expected Behavior

If possible, resolve to native types !?

Additional information

@loopback/cli version: 1.26.0

@derdeka derdeka added the bug label Nov 26, 2019
@raymondfeng
Copy link
Contributor

Please try to add type: 'string' to jsonSchema. I don't think TS emits the right design:type for id?: UUID. Can you check the corresponding transpliled js file under dist?

@bajtos
Copy link
Member

bajtos commented Nov 29, 2019

I see two aspects here:

  1. Our CLI tool should not use empty string as the type when emitting @param.path.(). When the type is not known, it can e.g. prompt the user to pick one of the types supported by our param decorators.

  2. As @raymondfeng mentioned, we are not able to infer target type when an alias is used. IMO, you should specify type inside @property decorator (not in jsonSchema):

  @property({
    id: true,
    type: 'string',
    generated: true,
    jsonSchema: {
      pattern: PATTERN_UUID,
    },
  })
  id?: UUID;

The second item above opens an interesting question - how to allow developers to create custom property types that can provide both TypeScript typings but also LB4 property metadata? Ideally, the part {type: 'string', jsonSchema: {pattern: PATTERN_UUID}} should be encapsulated in the UUID type. This is useful for MongoDB's ObjectID type too, which we want to treat as an alias for string (see #3456). I feel this discussion is out of scope of this issue though, let's open a new GH issue if we want to continue the discussion.

@bajtos bajtos added CLI Relations Model relations (has many, etc.) labels Nov 29, 2019
@derdeka
Copy link
Contributor Author

derdeka commented Dec 11, 2019

2. As @raymondfeng mentioned, we are not able to infer target type when an alias is used. IMO, you should specify type inside @property decorator (not in jsonSchema):

I had a look into my transpiled files now - the design type is String for type alias UUID in my case. The interferring of the target type seems to work somehow even with a type alias. I guess the cli tool could do this too. Needs more investigation.

@stale
Copy link

stale bot commented Dec 25, 2020

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

@stale stale bot added the stale label Dec 25, 2020
@stale
Copy link

stale bot commented Jul 14, 2021

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug CLI Relations Model relations (has many, etc.) stale
Projects
None yet
Development

No branches or pull requests

3 participants