-1

How do you deal with subclassing collection attributes in JSONModel?

Let's say I have these two endpoints with different responses of the same "product object".

domain.com/api/1.0/getProductList

domain.com/api/1.0/getProductDetails/productId

I wrote some example code below to show you my issue:

// ProductListModel

@interface ProductListModel : JSONModel

@property (nonatomic, strong) NSNumber *productId;
@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, strong) NSArray<OrderListModel> *orders;

@end

// ProductDetailModel

@interface ProductDetailModel : ProductListModel

@property (nonatomic, strong) NSURL *productImageUrl;
@property (nonatomic, strong) NSArray<OrderDetailModel> *orders;

@end

// OrderListModel

@protocol OrderListModel <NSObject>
@end

@interface OrderListModel : JSONModel

@property (nonatomic, strong) NSNumber *orderId;
@property (nonatomic, strong) NSNumber *price;

@end

// OrderDetailModel

@protocol OrderDetailModel <NSObject>
@end

@interface OrderDetailModel : OrderListModel

@property (nonatomic, strong) NSURL *orderImageUrl;

@end

The ProductDetailModel wants the same inherited attributes as ProductListModel, but it wants the orders array in the subclassed type. However this leads to a compiler warning:

Property type 'NSArray<OrderDetailModel> *' is incompatible with type
'NSArray<OrderListModel> *'  inherited from 'ProductListModel'

I found this related SO post but I'd rather not monkey patch the JSONModel library.

Edit #1:

This has been discussed in the #574, and #229 github issues before but requires a "type" string to determine what class to instantiate. This requires a change on the backend API.

Is there a way to do this without changing the backend API?

Community
  • 1
  • 1
Andy
  • 128
  • 1
  • 8
  • 1
    You can't override the property in the subclass to have a different type as it will violate the Liskov substitution principle. – Paulw11 Apr 10 '17 at 12:27
  • Of course, you are totally right.. I will have to introduce a ProductBaseModel that ProductListModel and ProductDetailModel inherits from. Thanks @Paulw11 – Andy Apr 10 '17 at 12:36

1 Answers1

0

You can't override the property in the subclass to have a different type as it will violate the Liskov substitution principle - @Paulw11

For future readers, here's how the updated example code would look like:

// ProductBaseModel

@interface ProductBaseModel : JSONModel

@property (nonatomic, strong) NSNumber *productId;
@property (nonatomic, strong) NSNumber *userId;

@end

// ProductListModel

@interface ProductListModel : ProductBaseModel

@property (nonatomic, strong) NSArray<OrderListModel> *orders;

@end

// ProductDetailModel

@interface ProductDetailModel : ProductBaseModel

@property (nonatomic, strong) NSURL *productImageUrl;
@property (nonatomic, strong) NSArray<OrderDetailModel> *orders;

@end

// OrderListModel

@protocol OrderListModel <NSObject>
@end

@interface OrderListModel : JSONModel

@property (nonatomic, strong) NSNumber *orderId;
@property (nonatomic, strong) NSNumber *price;

@end

// OrderDetailModel

@protocol OrderDetailModel <NSObject>
@end

@interface OrderDetailModel : OrderListModel

@property (nonatomic, strong) NSURL *orderImageUrl;

@end
Andy
  • 128
  • 1
  • 8