crossecore / crossecore-generator

Code generator for Java, C#, TypeScript and Swift
Apache License 2.0
12 stars 8 forks source link

[Typescript] The createXXXFromString assumes valid json #25

Open krissrex opened 3 years ago

krissrex commented 3 years ago

Description: I am trying to deserialize xmi. One of the EDataTypes I created (ActionId) has a value "ecore:new_child". When the code tries to create this ActionId, it runs this:

public createActionIdFromString(eDataType:EDataType, initialValue:string):string {
  return initialValue == null ? null : JSON.parse(initialValue);
}   

This assumes the initialValue (which is ecore:new_child) is valid json, and it crashes: image

It gets there via XmiResource.addEStructuralFeatures (line 169): var literalvalue = _this.factory.createFromString(etype, value); then via my Factory's public createFromString(eDataType:EDataType, initialValue:string) to the snipped shown first.


It would work if my initialvalue was wrapped in quotes: "ecore:new_child". But this EDataType was not stringified by the crossecore library, but the Eclipse IDE editor for xmi.

This is an excerpt of the xmi:

<availableActions
          id="ecore:new_child"
          name="New child"/>
schwichti commented 3 years ago

What is the instanceClassName of ActionId? I assume it is java.lang.String. I think this JSON stringify stuff has to be removed entirely as it is not compatible with the Java implementation of XmiResource. I need to look it up, but I can barely remember that in Java EDataTypes have to implement the Serializable interface. Maybe there is a way to port this to TypeScript.

schwichti commented 3 years ago

What the original Java implementation has this fallback: https://git.eclipse.org/c/emf/org.eclipse.emf.git/tree/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/impl/EFactoryImpl.java#n290. For now, I cannot see that it makes sense to port this logic to TypeScript. On the other side, the Javadoc says that clients have to hand-write createXyzFromString and createXyzToString http://download.eclipse.org/modeling/emf/emf/javadoc/2.7.0/org/eclipse/emf/ecore/EDataType.html#isSerializable(). In the long term this is my plan how to proceed:

In the meanwhile, I suppose that you override createXyzFromString by hand and are aware that every time you run the CrossEcore generator, your manual changes in *FactoryImpl are overriden (as long as @generated NOT is not supported)

schwichti commented 3 years ago

Some createXyzFromString and createXyzToString for the built-in EDataTypes of Ecore are implemented in 0.2.0 https://github.com/crossecore/ecore-typescript/pull/28