Skip to content

Commit

Permalink
Merge pull request #216 from contentful/bugfix/asset-url-localization
Browse files Browse the repository at this point in the history
 Debug Asset accessors not respecting fallback chain logic
  • Loading branch information
loudmouth committed Jul 29, 2018
2 parents 5e8440a + 93b13b0 commit 01c5589
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .env
@@ -1 +1 @@
CONTENTFUL_SDK_VERSION=2.2.0
CONTENTFUL_SDK_VERSION=2.2.1
2 changes: 1 addition & 1 deletion .envrc
@@ -1 +1 @@
export CONTENFUL_SDK_VERSION=2.2.0
export CONTENFUL_SDK_VERSION=2.2.1
10 changes: 9 additions & 1 deletion CHANGELOG.md
Expand Up @@ -10,7 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) starting from
## Table of contents

#### 2.x Releases
- `2.2.0` Releases - [2.2.0](#220)
- `2.2.0` Releases - [2.2.0](#220) | [2.2.1](#221)
- `2.1.0` Releases - [2.1.0](#210)
- `2.0.0` Releases - [2.0.0](#200)

Expand All @@ -31,6 +31,14 @@ This project adheres to [Semantic Versioning](http://semver.org/) starting from

---

## [`2.2.1`](https://github.com/contentful/contentful.swift/releases/tag/2.2.1)
Released on 2018-07-30

#### Fixed
- Accessing fields on assets was not respecting fallback chain logic when assets were requested with a multi-locale format. This is now fixed.

---

## [`2.2.0`](https://github.com/contentful/contentful.swift/releases/tag/2.2.0)
Released on 2018-06-13

Expand Down
4 changes: 2 additions & 2 deletions Cartfile.resolved
@@ -1,3 +1,3 @@
github "JensRavens/Interstellar" "2.2.0"
github "Quick/Nimble" "v7.1.2"
github "venmo/DVR" "v1.1.0"
github "Quick/Nimble" "v7.1.3"
github "venmo/DVR" "v1.2.0"
2 changes: 1 addition & 1 deletion Config.xcconfig
@@ -1 +1 @@
CONTENTFUL_SDK_VERSION=2.2.0
CONTENTFUL_SDK_VERSION=2.2.1
17 changes: 4 additions & 13 deletions Sources/Contentful/Asset.swift
Expand Up @@ -52,6 +52,7 @@ public class Asset: LocalizableResource, AssetProtocol {
/// The URL for the underlying media file. Returns nil if the url was omitted from the response (i.e. `select` operation in query)
/// or if the underlying media file is still processing with Contentful.
public var url: URL? {

guard let url = file?.url else { return nil }
return url
}
Expand All @@ -65,27 +66,17 @@ public class Asset: LocalizableResource, AssetProtocol {

/// The title of the asset. Optional for compatibility with `select` operator queries.
public var title: String? {
return localizedString(path: "title")
return fields["title"] as? String
}

/// Description of the asset. Optional for compatibility with `select` operator queries.
public var description: String? {
return localizedString(path: "description")
return fields["description"] as? String
}

/// Metadata describing the file associated with the asset. Optional for compatibility with `select` operator queries.
public var file: FileMetadata? {
let localizableValue = localizableFields["file"]
let value = localizableValue?[currentlySelectedLocale.code] as? FileMetadata
return value
}

// MARK: Private

private func localizedString(path: String) -> String? {
let localizableValue = localizableFields[path]
let value = localizableValue?[currentlySelectedLocale.code] as? String
return value
return fields["file"] as? FileMetadata
}
}

Expand Down
45 changes: 45 additions & 0 deletions Tests/ContentfulTests/Data/localizable-asset.json
@@ -0,0 +1,45 @@
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "cfexampleapi"
}
},
"id": "1x0xpXu4pSGS4OukSyWGUK",
"type": "Asset",
"createdAt": "2013-11-06T09:45:10.000Z",
"updatedAt": "2013-12-18T13:27:14.917Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 6
},
"fields": {
"title": {
"en-US": "Doge"
},
"description": {
"en-US": "nice picture"
},
"file": {
"en-US": {
"url": "//images.ctfassets.net/cfexampleapi/1x0xpXu4pSGS4OukSyWGUK/cc1239c6385428ef26f4180190532818/doge.jpg",
"details": {
"size": 522943,
"image": {
"width": 5800,
"height": 4350
}
},
"fileName": "doge.jpg",
"contentType": "image/jpeg"
}
}
}
}
19 changes: 19 additions & 0 deletions Tests/ContentfulTests/LocalizationTests.swift
Expand Up @@ -90,5 +90,24 @@ class LocalizationTests: XCTestCase {

waitForExpectations(timeout: 10.0, handler: nil)
}

func testWalkingFallbackchainOnAsset() {
let jsonDecoder = JSONDecoder.withoutLocalizationContext()
let localesJSONData = JSONDecodingTests.jsonData("all-locales")
let localesResponse = try! jsonDecoder.decode(ArrayResponse<Contentful.Locale>.self, from: localesJSONData)
jsonDecoder.update(with: LocalizationContext(locales: localesResponse.items)!)


let assetJSONData = JSONDecodingTests.jsonData("localizable-asset")
let asset = try! jsonDecoder.decode(Asset.self, from: assetJSONData)

expect(asset.sys.id).to(equal("1x0xpXu4pSGS4OukSyWGUK"))
expect(asset.urlString).to(equal("https://images.ctfassets.net/cfexampleapi/1x0xpXu4pSGS4OukSyWGUK/cc1239c6385428ef26f4180190532818/doge.jpg"))

asset.setLocale(withCode: "tlh")
expect(asset.urlString).to(equal("https://images.ctfassets.net/cfexampleapi/1x0xpXu4pSGS4OukSyWGUK/cc1239c6385428ef26f4180190532818/doge.jpg"))


}
}

0 comments on commit 01c5589

Please sign in to comment.