ServiceStack.Swift Support for array response

Hello,

When testing I encountered an issue where the requests with IReturn< Array > are not fully generated; from the documentation I found out that this is a Swift limitation. I have a few dozen requests that returns an array/list (I cannot change the requests) so I was wondering if their was any updates about this limitation. I was able to workaround most limitations of the Swift generator but I don’t think I can find a workaround for this one.

I was thinking that ServiceStack could automatically create a wrapper class for the arrays. Something like this:

ServiceStack would needs to generate the DTO with the correct Return typealias

public class Class1 : IReturn
{
    public typealias Return = ArrayOfSomeClass // using the wrapper for the array of SomeClass

    required public init(){}
}
protocol ArrayWrapper
{
    func fromArray(any:NSArray)
}

// This is the 'wrapper' class that ServiceStack could generate by replacing 'SomeClass' with the targeted class name
// SomeClass would be the returned type in IReturn<SomeClass[]>
public class ArrayOfSomeClass : JsonSerializable, ArrayWrapper
{
    public required init() {}
    public static var typeName:String { return "ArrayOfSomeClass" }
    public static var metadata = Metadata.create([])
    public var values = [SomeClass]()
    
    public func fromArray(array:NSArray)
    {
        for(item) in array
        {
            values.append(SomeClass.fromObject(item)!) 
        }
    }
	// I don't think that we need to implement these methods since this class will never be serialized or deserialized without using the fromArray method
    public static func fromObject(any:AnyObject) -> ArrayOfSomeClass? { return nil }
    public func toJson() -> String { return "" }
    public func toString() -> String { return toJson() }
    public static func fromString(string:String) -> ArrayOfSomeClass? { return nil }
}
    // Changes required to the fromJson method of the JsonServiceClient.swift

static func fromJson<T : JsonSerializable>(instance:T, json:String) -> T? {
	if instance is NSString || instance is String {
		if let value = json as? T {
			return value
		}
	}
	let parsedJson = parseJson(json);
	if let map = parsedJson as? NSDictionary {
		return populate(instance, map: map, propertiesMap: T.propertyMap)
	}
	
	// read the returned array
	if let array = parsedJson as? NSArray {
		if let arrayInstance = instance as? ArrayWrapper {
			arrayInstance.fromArray(array)
			return instance
		}
	}
	return nil
}

I dislike artificial Wrappers, they add indirection and magic to what should be a straight-forward Type translation which then also deviates from all other supported languages.

Just return a Response DTO containing the array result which removes the magic whilst maintaining parity and readability with source code for other supported languages. Effectively it’s an explicit user-defined wrapper you have complete control over, it’s also recommended practice since returning a vanilla JSON array in ajax is a security risk and it doesn’t allow for the Response DTO to be extended to return additional data/metadata without breaking clients.

Unfortunately I am not allowed to modify the requests.
I’ll try to figure out something for my situation.

Thanks

I played a bit with the code this weekend and I think I have found a solution you may find acceptable. I posted my findings here: https://github.com/ServiceStack/ServiceStack.Swift/issues/5

noted, tho still working on release notes for this release - earliest I can take a look is tomorrow afternoon.

Any updates on this? Sorry to be annoying with this but this issue and the date issue are the last remaining “pain points” before I can close my project.