@@ -161,10 +161,51 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Ty
161161 final TsType discriminantType = literals .isEmpty ()
162162 ? TsType .String
163163 : new TsType .UnionType (literals );
164- properties .add (0 , new TsPropertyModel (bean .getDiscriminantProperty (), discriminantType , settings .declarePropertiesAsReadOnly , /*ownProperty*/ true , null ));
164+ final TsModifierFlags modifiers = TsModifierFlags .None .setReadonly (settings .declarePropertiesAsReadOnly );
165+ properties .add (0 , new TsPropertyModel (bean .getDiscriminantProperty (), discriminantType , modifiers , /*ownProperty*/ true , null ));
165166 }
166167
167- return new TsBeanModel (bean .getOrigin (), TsBeanCategory .Data , isClass , beanIdentifier , typeParameters , parentType , bean .getTaggedUnionClasses (), interfaces , properties , null , null , bean .getComments ());
168+ final List <TsMethodModel > methods = isClass && settings .experimentalJsonDeserialization
169+ ? Arrays .asList (createDeserializationMethod (beanIdentifier , typeParameters , properties ))
170+ : null ;
171+ return new TsBeanModel (bean .getOrigin (), TsBeanCategory .Data , isClass , beanIdentifier , typeParameters , parentType , bean .getTaggedUnionClasses (), interfaces , properties , null , methods , bean .getComments ());
172+ }
173+
174+ private TsMethodModel createDeserializationMethod (Symbol beanIdentifier , List <TsType .GenericVariableType > typeParameters , List <TsPropertyModel > properties ) {
175+ final List <TsStatement > body = new ArrayList <>();
176+ body .add (new TsIfStatement (new TsPrefixUnaryExpression (TsUnaryOperator .Exclamation , new TsIdentifierReference ("data" )),
177+ Arrays .<TsStatement >asList (new TsReturnStatement (new TsIdentifierReference ("data" )))
178+ ));
179+ body .add (new TsVariableDeclarationStatement (
180+ /*const*/ true ,
181+ "instance" ,
182+ /*type*/ null ,
183+ new TsBinaryExpression (
184+ new TsIdentifierReference ("target" ),
185+ TsBinaryOperator .BarBar ,
186+ new TsNewExpression (new TsTypeReferenceExpression (new TsType .ReferenceType (beanIdentifier )), typeParameters , null )
187+ )
188+ ));
189+ for (TsPropertyModel property : properties ) {
190+ body .add (new TsExpressionStatement (new TsAssignmentExpression (
191+ new TsMemberExpression (new TsIdentifierReference ("instance" ), property .name ),
192+ new TsMemberExpression (new TsIdentifierReference ("data" ), property .name )
193+ )));
194+ }
195+ body .add (new TsReturnStatement (new TsIdentifierReference ("instance" )));
196+
197+ final TsType .ReferenceType dataType = typeParameters .isEmpty ()
198+ ? new TsType .ReferenceType (beanIdentifier )
199+ : new TsType .GenericReferenceType (beanIdentifier , typeParameters );
200+ return new TsMethodModel (
201+ "fromData" ,
202+ TsModifierFlags .None .setStatic (),
203+ typeParameters ,
204+ Arrays .asList (new TsParameterModel ("data" , dataType ), new TsParameterModel ("target" , dataType .optional ())),
205+ dataType ,
206+ body ,
207+ null
208+ );
168209 }
169210
170211 private List <TsPropertyModel > processProperties (SymbolTable symbolTable , Model model , BeanModel bean , String prefix , String suffix ) {
@@ -223,7 +264,8 @@ private static boolean containsProperty(List<TsPropertyModel> properties, String
223264 private TsPropertyModel processProperty (SymbolTable symbolTable , BeanModel bean , PropertyModel property , String prefix , String suffix ) {
224265 final TsType type = typeFromJava (symbolTable , property .getType (), property .getName (), bean .getOrigin ());
225266 final TsType tsType = property .isOptional () ? type .optional () : type ;
226- return new TsPropertyModel (prefix + property .getName () + suffix , tsType , settings .declarePropertiesAsReadOnly , false , property .getComments ());
267+ final TsModifierFlags modifiers = TsModifierFlags .None .setReadonly (settings .declarePropertiesAsReadOnly );
268+ return new TsPropertyModel (prefix + property .getName () + suffix , tsType , modifiers , /*ownProperty*/ false , property .getComments ());
227269 }
228270
229271 private TsEnumModel processEnum (SymbolTable symbolTable , EnumModel enumModel ) {
@@ -356,22 +398,23 @@ private TsModel createJaxrsClients(SymbolTable symbolTable, TsModel tsModel, Jax
356398
357399 // HttpClient interface
358400 tsModel .getBeans ().add (new TsBeanModel (null , TsBeanCategory .ServicePrerequisite , false , httpClientSymbol , typeParameters , null , null , null , null , null , Arrays .asList (
359- new TsMethodModel ("request" , new TsType . GenericReferenceType ( responseSymbol , TsType . Any ) , Arrays .asList (
401+ new TsMethodModel ("request" , TsModifierFlags . None , null , Arrays .asList (
360402 new TsParameterModel ("requestConfig" , new TsType .ObjectType (
361403 new TsProperty ("method" , TsType .String ),
362404 new TsProperty ("url" , TsType .String ),
363405 new TsProperty ("queryParams" , new TsType .OptionalType (TsType .Any )),
364406 new TsProperty ("data" , new TsType .OptionalType (TsType .Any )),
365407 optionsType != null ? new TsProperty ("options" , new TsType .OptionalType (optionsType )) : null
366408 ))
367- ), null , null )
409+ ), new TsType . GenericReferenceType ( responseSymbol , TsType . Any ), null , null )
368410 ), null ));
369411
370412 // application client classes
371413 final TsType .ReferenceType httpClientType = optionsGenericVariable != null
372414 ? new TsType .GenericReferenceType (httpClientSymbol , optionsGenericVariable )
373415 : new TsType .ReferenceType (httpClientSymbol );
374416 final TsConstructorModel constructor = new TsConstructorModel (
417+ TsModifierFlags .None ,
375418 Arrays .asList (new TsParameterModel (TsAccessibilityModifier .Protected , "httpClient" , httpClientType )),
376419 Collections .<TsStatement >emptyList (),
377420 null
@@ -524,7 +567,7 @@ private TsMethodModel processJaxrsMethod(SymbolTable symbolTable, String pathPre
524567 body = null ;
525568 }
526569 // method
527- final TsMethodModel tsMethodModel = new TsMethodModel (method .getName () + nameSuffix , wrappedReturnType , parameters , body , comments );
570+ final TsMethodModel tsMethodModel = new TsMethodModel (method .getName () + nameSuffix , TsModifierFlags . None , null , parameters , wrappedReturnType , body , comments );
528571 return tsMethodModel ;
529572 }
530573
@@ -554,7 +597,7 @@ private TsModel transformDates(SymbolTable symbolTable, TsModel tsModel) {
554597 final LinkedHashSet <TsAliasModel > typeAliases = new LinkedHashSet <>(tsModel .getTypeAliases ());
555598 final TsModel model = transformBeanPropertyTypes (tsModel , new TsType .Transformer () {
556599 @ Override
557- public TsType transform (TsType type ) {
600+ public TsType transform (TsType . Context context , TsType type ) {
558601 if (type == TsType .Date ) {
559602 if (settings .mapDate == DateMapping .asNumber ) {
560603 typeAliases .add (dateAsNumber );
@@ -590,7 +633,7 @@ private TsModel inlineEnums(final TsModel tsModel, final SymbolTable symbolTable
590633 final Set <TsAliasModel > inlinedAliases = new LinkedHashSet <>();
591634 final TsModel newTsModel = transformBeanPropertyTypes (tsModel , new TsType .Transformer () {
592635 @ Override
593- public TsType transform (TsType tsType ) {
636+ public TsType transform (TsType . Context context , TsType tsType ) {
594637 if (tsType instanceof TsType .EnumReferenceType ) {
595638 final TsAliasModel alias = tsModel .getTypeAlias (getOriginClass (symbolTable , tsType ));
596639 if (alias != null ) {
@@ -638,9 +681,9 @@ private TsModel createAndUseTaggedUnions(final SymbolTable symbolTable, TsModel
638681 // use tagged unions
639682 final TsModel model = transformBeanPropertyTypes (tsModel , new TsType .Transformer () {
640683 @ Override
641- public TsType transform (TsType tsType ) {
684+ public TsType transform (TsType . Context context , TsType tsType ) {
642685 final Class <?> cls = getOriginClass (symbolTable , tsType );
643- if (cls != null && !(tsType instanceof TsType .GenericReferenceType )) {
686+ if (cls != null && !cls . equals ( context . bean . getOrigin ()) && ! (tsType instanceof TsType .GenericReferenceType )) {
644687 final Symbol unionSymbol = symbolTable .hasSymbol (cls , "Union" );
645688 if (unionSymbol != null ) {
646689 return new TsType .ReferenceType (unionSymbol );
@@ -691,20 +734,21 @@ private static void addOrderedClass(SymbolTable symbolTable, TsModel tsModel, Ts
691734 private static TsModel transformBeanPropertyTypes (TsModel tsModel , TsType .Transformer transformer ) {
692735 final List <TsBeanModel > newBeans = new ArrayList <>();
693736 for (TsBeanModel bean : tsModel .getBeans ()) {
737+ final TsType .Context context = new TsType .Context (bean );
694738 final List <TsPropertyModel > newProperties = new ArrayList <>();
695739 for (TsPropertyModel property : bean .getProperties ()) {
696- final TsType newType = TsType .transformTsType (property .getTsType (), transformer );
740+ final TsType newType = TsType .transformTsType (context , property .getTsType (), transformer );
697741 newProperties .add (property .setTsType (newType ));
698742 }
699743 final List <TsMethodModel > newMethods = new ArrayList <>();
700744 for (TsMethodModel method : bean .getMethods ()) {
701745 final List <TsParameterModel > newParameters = new ArrayList <>();
702746 for (TsParameterModel parameter : method .getParameters ()) {
703- final TsType newParameterType = TsType .transformTsType (parameter .getTsType (), transformer );
747+ final TsType newParameterType = TsType .transformTsType (context , parameter .getTsType (), transformer );
704748 newParameters .add (new TsParameterModel (parameter .getAccessibilityModifier (), parameter .getName (), newParameterType ));
705749 }
706- final TsType newReturnType = TsType .transformTsType (method .getReturnType (), transformer );
707- newMethods .add (new TsMethodModel (method .getName (), newReturnType , newParameters , method .getBody (), method .getComments ()));
750+ final TsType newReturnType = TsType .transformTsType (context , method .getReturnType (), transformer );
751+ newMethods .add (new TsMethodModel (method .getName (), method . getModifiers (), method . getTypeParameters (), newParameters , newReturnType , method .getBody (), method .getComments ()));
708752 }
709753 newBeans .add (bean .withProperties (newProperties ).withMethods (newMethods ));
710754 }
0 commit comments