-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Ensure the same date format string is used throughout the generated code (use the one set in Configuration.swift). Ensure the same date formatter options are used when encoding dates as well as decoding dates. If a consumer has set their own date formatter on CodableHelper, use that when encoding dates too. Adds DateFormatTests to the SWIFT4 unit tests. Updates the SWIFT4 petstore samples
- Loading branch information
1 parent
7564d62
commit 653601b
Showing
47 changed files
with
538 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...lient/petstore/swift4/default/SwaggerClientTests/SwaggerClientTests/DateFormatTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// DateFormatTests.swift | ||
// SwaggerClientTests | ||
// | ||
// Created by James on 14/11/2018. | ||
// Copyright © 2018 Swagger. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import PetstoreClient | ||
@testable import SwaggerClient | ||
|
||
class DateFormatTests: XCTestCase { | ||
|
||
struct DateTest: Codable { | ||
let date: Date | ||
} | ||
|
||
override func setUp() { | ||
super.setUp() | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
override func tearDown() { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
super.tearDown() | ||
} | ||
|
||
func testEncodeToJSONAlwaysResultsInUTCEncodedDate() { | ||
var dateComponents = DateComponents() | ||
dateComponents.calendar = Calendar(identifier: .gregorian) | ||
dateComponents.year = 2018 | ||
dateComponents.month = 11 | ||
dateComponents.day = 14 | ||
dateComponents.hour = 11 | ||
dateComponents.minute = 35 | ||
dateComponents.second = 43 | ||
dateComponents.nanosecond = 500 | ||
|
||
// Testing a date with a timezone of +00:00 (UTC) | ||
dateComponents.timeZone = TimeZone(secondsFromGMT: 0) | ||
XCTAssert(dateComponents.isValidDate) | ||
|
||
guard let utcDate = dateComponents.date else { | ||
XCTFail("Couldn't get a valid date") | ||
return | ||
} | ||
|
||
var encodedDate = utcDate.encodeToJSON() as! String | ||
XCTAssert(encodedDate.hasSuffix("Z")) | ||
|
||
// test with a positive timzone offset from UTC | ||
dateComponents.timeZone = TimeZone(secondsFromGMT: 60 * 60) // +01:00 | ||
XCTAssert(dateComponents.isValidDate) | ||
|
||
guard let nonUTCDate1 = dateComponents.date else { | ||
XCTFail("Couldn't get a valid date") | ||
return | ||
} | ||
|
||
encodedDate = nonUTCDate1.encodeToJSON() as! String | ||
XCTAssert(encodedDate.hasSuffix("Z")) | ||
|
||
// test with a negative timzone offset from UTC | ||
dateComponents.timeZone = TimeZone(secondsFromGMT: -(60 * 60)) // -01:00 | ||
XCTAssert(dateComponents.isValidDate) | ||
|
||
guard let nonUTCDate2 = dateComponents.date else { | ||
XCTFail("Couldn't get a valid date") | ||
return | ||
} | ||
|
||
encodedDate = nonUTCDate2.encodeToJSON() as! String | ||
XCTAssert(encodedDate.hasSuffix("Z")) | ||
} | ||
|
||
func testCodableAlwaysResultsInUTCEncodedDate() { | ||
let jsonData = "{\"date\":\"1970-01-01T00:00:00.000Z\"}".data(using: .utf8)! | ||
let decodeResult = CodableHelper.decode(DateTest.self, from: jsonData) | ||
XCTAssert(decodeResult.decodableObj != nil && decodeResult.error == nil) | ||
|
||
var dateComponents = DateComponents() | ||
dateComponents.calendar = Calendar(identifier: .gregorian) | ||
dateComponents.year = 1970 | ||
dateComponents.month = 01 | ||
dateComponents.day = 01 | ||
dateComponents.hour = 00 | ||
dateComponents.minute = 00 | ||
dateComponents.second = 00 | ||
|
||
// Testing a date with a timezone of +00:00 (UTC) | ||
dateComponents.timeZone = TimeZone(secondsFromGMT: 0) | ||
XCTAssert(dateComponents.isValidDate) | ||
|
||
guard let date = dateComponents.date else { | ||
XCTFail("Couldn't get a valid date") | ||
return | ||
} | ||
|
||
let dateTest = DateTest(date: date) | ||
let encodeResult = CodableHelper.encode(dateTest) | ||
XCTAssert(encodeResult.data != nil && encodeResult.error == nil) | ||
guard let jsonString = String(data: encodeResult.data!, encoding: .utf8) else { | ||
XCTFail("Unable to convert encoded data to string.") | ||
return | ||
} | ||
|
||
let exampleJSONString = "{\"date\":\"1970-01-01T00:00:00.000Z\"}" | ||
XCTAssert(jsonString == exampleJSONString, "Encoded JSON String: \(jsonString) should match: \(exampleJSONString)") | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
samples/client/petstore/swift4/objcCompatible/.openapi-generator/VERSION
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.3.0-SNAPSHOT | ||
3.3.3-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.