castle icon indicating copy to clipboard operation
castle copied to clipboard

Externals imports possibly broken?

Open matthewchngshopback opened this issue 2 years ago • 4 comments

Context

Using @ovotech/[email protected] and executing

npx avro-ts address.avsc.json create-user.avsc.json

where both the schemas were copied from avro-ts/examples

Expected

Generated TypeScript files should contain no errors in their imports of external references

Actual

I got these generated output with errors in imports of external reference to my.namespace.data.Address

create-user.avsc.json.ts has

  1. import which wasn't exported from address.avsc.json.ts; and
  2. import is missing alias identifier after the as; and
  3. broken CreateUser.address reference with an undefined namespace.
/* eslint-disable @typescript-eslint/no-namespace */

import { type MyNamespaceDataAddress as  } from "./address.avsc.json";     (1), (2)

export type CreateUser = MyNamespaceMessages.CreateUser;

export namespace MyNamespaceMessages {
    export const CreateUserName = "my.namespace.messages.CreateUser";
    export interface CreateUser {
        userId: string;
        name: string;
        address: MyNamespaceDataAddress.Address;                           (3)
    }
}

address.avsc.json looks ok and the namespace exported is actually MyNamespaceData rather than MyNamespaceDataAddress which is imported by the create-user.avsc.json.ts above.

/* eslint-disable @typescript-eslint/no-namespace */

export type Address = MyNamespaceData.Address;

export namespace MyNamespaceData {
    export const AddressName = "my.namespace.data.Address";
    export interface Address {
        street: string;
        zipcode: string;
        country: string;
    }
}

matthewchngshopback avatar Mar 22 '22 16:03 matthewchngshopback

+1 I'm experiencing this same issue.

cafreeman avatar Jun 16 '22 19:06 cafreeman

Hi, 

I think the main problem come from Typescript breaking change on version 4.5

Base on what I found after I install @ovotech/[email protected]

With @[email protected] the code where it create import statement specifier is the following

return ts.factory.createImportSpecifier(
       item.as ? exports.Node.Identifier(item.name) : undefined, 
       item.as ? exports.Node.Identifier(item.as) : exports.Node.Identifier(item.name))
})

Once I update the call to the following, the import statement specifier is create correctly



return ts.factory.createImportSpecifier(
       false, 
       item.as ? exports.Node.Identifier(item.name) : undefined, 
       item.as ? exports.Node.Identifier(item.as) : exports.Node.Identifier(item.name))
})


I think the solution is either force typescript version to be 4.1.2
 or upgrade @[email protected] to version 0.20.0 which already fix API breaking change on typescript 4.5 (https://github.com/ovotech/laminar/blob/d197413e78625b710480de35e99e5d5d9d13a6cc/packages/ts-compose/src/node.ts#L205)

rockt987 avatar Jul 19 '22 07:07 rockt987

Both are options for sure - if anybody wants to try a PR I would be eternally grateful, don't have much time to fix those issues lately.

ivank avatar Jul 19 '22 12:07 ivank

There is a workaround to temporary solve this issue while we wait for dependencies upgrade. Try to force correct version through resolutions in package.json:

  "resolutions": {
    "@ovotech/ts-compose": "^0.20.0"
  }

gwer avatar Jan 10 '24 15:01 gwer