Executing yo hyperledger-composer fails to correctly import model

261 views Asked by At

Building a simple hyperledger-composer based app and using model definitions similar to those in the vehicle-lifecycle sample code. Specifically, I have two model files: base.cto

namespace composer.base

abstract participant Member {
  o String companyName
}

and sample.cto (partial listing)

namespace org.acme.Z2BTestNetwork
import composer.base.*
import composer.events.*

participant Buyer identified by buyerID extends Member{
    o String buyerID
}
participant Seller identified by sellerID extends Member{
    o String sellerID
}

asset Order identified by orderNumber {
    o String orderNumber
    o String[] items

I am able to successfully build the network using this structure and test the network, both using the composer-rest-server service and the Bluemix based composer. However a yo-generated app is unable to find "Member", apparently not importing the base.cto file during the generate and build process. yo generates a .ts file for each of the files in the model folder for this network. The .ts file which corresponds to 'sample.cto' contains the following, however it is missing the abstract definition from the base.cto file (which is in a different .ts file) and it is missing any link to that file.

import {Asset} from './org.hyperledger.composer.system';
import {Participant} from './org.hyperledger.composer.system';
import {Transaction} from './org.hyperledger.composer.system';
import {Event} from './org.hyperledger.composer.system';
// export namespace org.acme.Z2BTestNetwork{
   export class Buyer extends Member {
      buyerID: string;
   }
   export class Seller extends Member {
      sellerID: string;
   }
   export class Shipper extends Member {
      shipperID: string;
   }
   export class Provider extends Member {
      providerID: string;
   }
   export class FinanceCo extends Member {
      financeCoID: string;
   }
   export class Order extends Asset {
      orderNumber: string;
      items: string[];

An additional import statement is required for this app to function correctly.

import {Member} from './org.acme.Z2BTestNetwork.base';

This appears to be a bug in this implementation of yo for hyperledger-composer. Any recommendations on automating a fix for this?

Note if the yo option to use namespaces is selected, then only a single file is generated from sample.cto. The base.cto file no longer causes a .ts file to be generated; however the definitions from the base.cto file are not used and the app still fails to load because of the missing abstract definition for Member defined in the base.cto file.

2

There are 2 answers

1
Bob Dill On BEST ANSWER

Until the bug is fixed, there is a manual work-around. That is to add in the necessary import statements to the base .ts file - in my case, that would be the org.acme.Z2BTestNetwork.ts file. An import is required for each of the classes which you will need in your target environment. As I was only missing a single class (Member), only one import statement was required.

It would be possible to build a brute force bash exec to merge the necessary files together as an alternative until this is resolved if there were many required import statements.

3
Dan Selman On

This does indeed look like a bug. I will investigate, create an issue and fix.