Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make moduleObject.mustache confirms to JSONEncodable. #11202

Merged
merged 19 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ extension Date: JSONEncodable {
func encodeToJSON() -> Any {
return CodableHelper.dateFormatter.string(from: self)
}
}{{/useVapor}}{{#generateModelAdditionalProperties}}
}

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
}
}
{{/useVapor}}{{#generateModelAdditionalProperties}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x0c just a small nit, could you please make this in one line to improve the generated code?

}{{/useVapor}}{{#generateModelAdditionalProperties}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 8b1529f

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, could you please update the sample projects also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last commit brought a lot of undesired changes, could you please revert it?
Maybe you need to first merge the master branch into yours and then run the following commands.

./mvnw clean package 
./bin/generate-samples.sh
./bin/utils/export_docs_generators.sh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 5a1661b and 51fe33f

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why some enums are not JSONEncodable?
Is there any issue that you found?

Copy link
Contributor Author

@0x0c 0x0c Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since enum value that confirms RawRepresentable a.k.a primitive value is already confirm JSONEncodable here and here, it seems to that modelEnum.mustache and modelInlineEnumDeclaration.mustache should not confirm JSONCodable. If confirm explicitly, Xcode could not build generated source code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, thanks 👍


extension String: CodingKey {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum {{classname}}: {{dataType}}, {{#useVapor}}Content, Hashable{{/useVapor}}{{^useVapor}}Codable{{/useVapor}}, CaseIterable{{#enumUnknownDefaultCase}}{{#isInteger}}, CaseIterableDefaultsLast{{/isInteger}}{{#isFloat}}, CaseIterableDefaultsLast{{/isFloat}}{{#isDouble}}, CaseIterableDefaultsLast{{/isDouble}}{{#isString}}, CaseIterableDefaultsLast{{/isString}}{{/enumUnknownDefaultCase}} {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum {{classname}}: {{dataType}}, {{#useVapor}}Content, Hashable{{/useVapor}}{{^useVapor}}Codable, JSONEncodable{{/useVapor}}, CaseIterable{{#enumUnknownDefaultCase}}{{#isInteger}}, CaseIterableDefaultsLast{{/isInteger}}{{#isFloat}}, CaseIterableDefaultsLast{{/isFloat}}{{#isDouble}}, CaseIterableDefaultsLast{{/isDouble}}{{#isString}}, CaseIterableDefaultsLast{{/isString}}{{/enumUnknownDefaultCase}} {
{{#allowableValues}}
{{#enumVars}}
case {{{name}}} = {{{value}}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum {{enumName}}: {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}String{{/isContainer}}, {{#useVapor}}Content, Hashable{{/useVapor}}{{^useVapor}}Codable{{/useVapor}}, CaseIterable{{#enumUnknownDefaultCase}}{{#isInteger}}, CaseIterableDefaultsLast{{/isInteger}}{{#isFloat}}, CaseIterableDefaultsLast{{/isFloat}}{{#isDouble}}, CaseIterableDefaultsLast{{/isDouble}}{{#isString}}, CaseIterableDefaultsLast{{/isString}}{{#isContainer}}, CaseIterableDefaultsLast{{/isContainer}}{{/enumUnknownDefaultCase}} {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum {{enumName}}: {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}String{{/isContainer}}, {{#useVapor}}Content, Hashable{{/useVapor}}{{^useVapor}}Codable, JSONEncodable{{/useVapor}}, CaseIterable{{#enumUnknownDefaultCase}}{{#isInteger}}, CaseIterableDefaultsLast{{/isInteger}}{{#isFloat}}, CaseIterableDefaultsLast{{/isFloat}}{{#isDouble}}, CaseIterableDefaultsLast{{/isDouble}}{{#isString}}, CaseIterableDefaultsLast{{/isString}}{{#isContainer}}, CaseIterableDefaultsLast{{/isContainer}}{{/enumUnknownDefaultCase}} {
{{#allowableValues}}
{{#enumVars}}
case {{{name}}} = {{{value}}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#useClasses}}final class{{/useClasses}}{{^useClasses}}struct{{/useClasses}} {{{classname}}}: {{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable{{/useVapor}}{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}} {
{{/objcCompatible}}{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable {
{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#useClasses}}final class{{/useClasses}}{{^useClasses}}struct{{/useClasses}} {{{classname}}}: {{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable, JSONEncodable{{/useVapor}}{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}}{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x0c another nit that I found is that at the end of the line, there is a space before the final { that was acidently removed and is creating some noise for example in the vapor client that shouldn't have any changes with this PR.
Could you please restore that space, to avoid noise in the generated code?
This command should generate the Swift sample projects.

./bin/generate-samples.sh bin/configs/swift5-*
`
``

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 672df35 and 8962b8b

{{/objcCompatible}}{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable, JSONEncodable {
{{/objcCompatible}}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also add the JSONEncodable conformance after Codable in the following files, please?
modelEnum.mustache
modelInlineEnumDeclaration.mustache
modelOneOf.mustache

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with d855a09 and 4929d5b .

{{#allVars}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum {{classname}}: {{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}}{{/useVapor}} {
public enum {{classname}}: {{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable, JSONEncodable{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}}{{/useVapor}} {
{{#oneOf}}
case type{{.}}({{.}})
{{/oneOf}}
Expand Down
44 changes: 44 additions & 0 deletions modules/openapi-generator/src/test/resources/jsoncodable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
openapi: 3.0.0
info:
title: test
version: '1.0'
servers:
- url: 'http://localhost:3000'
paths:
/postModel:
post:
summary: Create New User
operationId: post-user
responses:
'200':
description: User Created
content:
application/json:
schema:
$ref: '#/components/schemas/User'
examples: {}
'400':
description: Missing Required Information
description: Create a new user.
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/Request'
parameters: []
components:
schemas:
User:
title: User
type: object
description: ''
x-examples: {}
properties:
integerValue:
type: integer
Request:
title: Request
type: object
properties:
user1:
$ref: '#/components/schemas/User'
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ extension Date: JSONEncodable {
}
}

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
}
}


extension String: CodingKey {

public var stringValue: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct AdditionalPropertiesClass: Codable, Hashable {
public struct AdditionalPropertiesClass: Codable, JSONEncodable, Hashable{

public var mapString: [String: String]?
public var mapMapString: [String: [String: String]]?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct Animal: Codable, Hashable {
public struct Animal: Codable, JSONEncodable, Hashable{

public var className: String
public var color: String? = "red"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct ApiResponse: Codable, Hashable {
public struct ApiResponse: Codable, JSONEncodable, Hashable{

public var code: Int?
public var type: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct ArrayOfArrayOfNumberOnly: Codable, Hashable {
public struct ArrayOfArrayOfNumberOnly: Codable, JSONEncodable, Hashable{

public var arrayArrayNumber: [[Double]]?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct ArrayOfNumberOnly: Codable, Hashable {
public struct ArrayOfNumberOnly: Codable, JSONEncodable, Hashable{

public var arrayNumber: [Double]?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct ArrayTest: Codable, Hashable {
public struct ArrayTest: Codable, JSONEncodable, Hashable{

public var arrayOfString: [String]?
public var arrayArrayOfInteger: [[Int64]]?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct Capitalization: Codable, Hashable {
public struct Capitalization: Codable, JSONEncodable, Hashable{

public var smallCamel: String?
public var capitalCamel: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct Cat: Codable, Hashable {
public struct Cat: Codable, JSONEncodable, Hashable{

public var className: String
public var color: String? = "red"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct CatAllOf: Codable, Hashable {
public struct CatAllOf: Codable, JSONEncodable, Hashable{

public var declawed: Bool?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct Category: Codable, Hashable {
public struct Category: Codable, JSONEncodable, Hashable{

public var id: Int64?
public var name: String? = "default-name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AnyCodable
#endif

/** Model for testing model with \"_class\" property */
public struct ClassModel: Codable, Hashable {
public struct ClassModel: Codable, JSONEncodable, Hashable{

public var _class: String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct Client: Codable, Hashable {
public struct Client: Codable, JSONEncodable, Hashable{

public var client: String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct Dog: Codable, Hashable {
public struct Dog: Codable, JSONEncodable, Hashable{

public var className: String
public var color: String? = "red"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct DogAllOf: Codable, Hashable {
public struct DogAllOf: Codable, JSONEncodable, Hashable{

public var breed: String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import Foundation
import AnyCodable
#endif

public struct EnumArrays: Codable, Hashable {
public struct EnumArrays: Codable, JSONEncodable, Hashable{

public enum JustSymbol: String, Codable, CaseIterable {
public enum JustSymbol: String, Codable, JSONEncodable, CaseIterable {
case greaterThanOrEqualTo = ">="
case dollar = "$"
}
public enum ArrayEnum: String, Codable, CaseIterable {
public enum ArrayEnum: String, Codable, JSONEncodable, CaseIterable {
case fish = "fish"
case crab = "crab"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public enum EnumClass: String, Codable, CaseIterable {
public enum EnumClass: String, Codable, JSONEncodable, CaseIterable {
case abc = "_abc"
case efg = "-efg"
case xyz = "(xyz)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import Foundation
import AnyCodable
#endif

public struct EnumTest: Codable, Hashable {
public struct EnumTest: Codable, JSONEncodable, Hashable{

public enum EnumString: String, Codable, CaseIterable {
public enum EnumString: String, Codable, JSONEncodable, CaseIterable {
case upper = "UPPER"
case lower = "lower"
case empty = ""
}
public enum EnumStringRequired: String, Codable, CaseIterable {
public enum EnumStringRequired: String, Codable, JSONEncodable, CaseIterable {
case upper = "UPPER"
case lower = "lower"
case empty = ""
}
public enum EnumInteger: Int, Codable, CaseIterable {
public enum EnumInteger: Int, Codable, JSONEncodable, CaseIterable {
case _1 = 1
case number1 = -1
}
public enum EnumNumber: Double, Codable, CaseIterable {
public enum EnumNumber: Double, Codable, JSONEncodable, CaseIterable {
case _11 = 1.1
case number12 = -1.2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AnyCodable
#endif

/** Must be named `File` for test. */
public struct File: Codable, Hashable {
public struct File: Codable, JSONEncodable, Hashable{

/** Test capitalization */
public var sourceURI: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct FileSchemaTestClass: Codable, Hashable {
public struct FileSchemaTestClass: Codable, JSONEncodable, Hashable{

public var file: File?
public var files: [File]?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct FormatTest: Codable, Hashable {
public struct FormatTest: Codable, JSONEncodable, Hashable{

public var integer: Int?
public var int32: Int?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct HasOnlyReadOnly: Codable, Hashable {
public struct HasOnlyReadOnly: Codable, JSONEncodable, Hashable{

public var bar: String?
public var foo: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct List: Codable, Hashable {
public struct List: Codable, JSONEncodable, Hashable{

public var _123list: String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Foundation
import AnyCodable
#endif

public struct MapTest: Codable, Hashable {
public struct MapTest: Codable, JSONEncodable, Hashable{

public enum MapOfEnumString: String, Codable, CaseIterable {
public enum MapOfEnumString: String, Codable, JSONEncodable, CaseIterable {
case upper = "UPPER"
case lower = "lower"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct MixedPropertiesAndAdditionalPropertiesClass: Codable, Hashable {
public struct MixedPropertiesAndAdditionalPropertiesClass: Codable, JSONEncodable, Hashable{

public var uuid: UUID?
public var dateTime: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AnyCodable
#endif

/** Model for testing model name starting with number */
public struct Model200Response: Codable, Hashable {
public struct Model200Response: Codable, JSONEncodable, Hashable{

public var name: Int?
public var _class: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AnyCodable
#endif

/** Model for testing model name same as property name */
public struct Name: Codable, Hashable {
public struct Name: Codable, JSONEncodable, Hashable{

public var name: Int
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AnyCodable
#endif

public struct NumberOnly: Codable, Hashable {
public struct NumberOnly: Codable, JSONEncodable, Hashable{

public var justNumber: Double?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Foundation
import AnyCodable
#endif

public struct Order: Codable, Hashable {
public struct Order: Codable, JSONEncodable, Hashable{

public enum Status: String, Codable, CaseIterable {
public enum Status: String, Codable, JSONEncodable, CaseIterable {
case placed = "placed"
case approved = "approved"
case delivered = "delivered"
Expand Down
Loading