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 2 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 @@ -206,3 +206,13 @@ extension Set: RequestDecodable where Element: Content {
extension Set: Content where Element: Content { }

extension AnyCodable: Content {}{{/useVapor}}

extension JSONEncodable where Self: Encodable {
Copy link
Contributor

@4brunu 4brunu Jan 4, 2022

Choose a reason for hiding this comment

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

@0x0c could you please move this entire block of code to line 63, so that it's not generated when using vapor, for CI to pass?

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 ebd7d01

func encodeToJSON() -> Any {
let encoder = JSONEncoder()
Copy link
Contributor

Choose a reason for hiding this comment

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

@0x0c here instead of creating a new JSONEncoder, please use CodableHelper.jsonEncoder

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 1c06b4d

guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.base64EncodedString(options: Data.Base64EncodingOptions())
Copy link
Contributor

Choose a reason for hiding this comment

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

@0x0c I'm a little worried about this.
When do we want to encode json as base64?
Only in multipart/form-data requests?
Can't we just return the json in Data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@4brunu According to generated code, Data can encode to json as follows.

extension Data: JSONEncodable {
    func encodeToJSON() -> Any {
        return self.base64EncodedString(options: Data.Base64EncodingOptions())
    }
}

This code is implemented here. Therefore, I followed this code and encode json as base 64.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see.
Could you please reuse that extension please?
return data.encodeToJSON()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated line 216 so please check 7836e27

}
}
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{{/useVapor}}{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}}, JSONEncodable {
Copy link
Contributor

Choose a reason for hiding this comment

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

@0x0c to make the CI pass, could you please update this line to be

{{^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}}{

Because when using Vapor, we can't make the model conform to JSONEncodable

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 aae8e6d

{{/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
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 @@ -156,3 +156,13 @@ extension HTTPURLResponse {
return (200 ..< 300).contains(statusCode)
}
}

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = JSONEncoder()
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.base64EncodedString(options: Data.Base64EncodingOptions())
}
}
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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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 \&quot;_class\&quot; property */
public struct ClassModel: Codable, Hashable {
public struct ClassModel: Codable, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

public var breed: 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 EnumArrays: Codable, Hashable {
public struct EnumArrays: Codable, Hashable, JSONEncodable {

public enum JustSymbol: String, Codable, CaseIterable {
case greaterThanOrEqualTo = ">="
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 EnumTest: Codable, Hashable {
public struct EnumTest: Codable, Hashable, JSONEncodable {

public enum EnumString: String, Codable, CaseIterable {
case upper = "UPPER"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AnyCodable
#endif

/** Must be named &#x60;File&#x60; for test. */
public struct File: Codable, Hashable {
public struct File: Codable, Hashable, JSONEncodable {

/** 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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

public var _123list: 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 MapTest: Codable, Hashable {
public struct MapTest: Codable, Hashable, JSONEncodable {

public enum MapOfEnumString: String, Codable, CaseIterable {
case upper = "UPPER"
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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

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, Hashable, JSONEncodable {

public var name: Int
public var snakeCase: 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 NumberOnly: Codable, Hashable {
public struct NumberOnly: Codable, Hashable, JSONEncodable {

public var justNumber: 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 Order: Codable, Hashable {
public struct Order: Codable, Hashable, JSONEncodable {

public enum Status: String, Codable, CaseIterable {
case placed = "placed"
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 OuterComposite: Codable, Hashable {
public struct OuterComposite: Codable, Hashable, JSONEncodable {

public var myNumber: Double?
public var myString: 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 Pet: Codable, Hashable {
public struct Pet: Codable, Hashable, JSONEncodable {

public enum Status: String, Codable, CaseIterable {
case available = "available"
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 ReadOnlyFirst: Codable, Hashable {
public struct ReadOnlyFirst: Codable, Hashable, JSONEncodable {

public var bar: String?
public var baz: 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 reserved words */
public struct Return: Codable, Hashable {
public struct Return: Codable, Hashable, JSONEncodable {

public var _return: 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 SpecialModelName: Codable, Hashable {
public struct SpecialModelName: Codable, Hashable, JSONEncodable {

public var specialPropertyName: 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 StringBooleanMap: Codable, Hashable {
public struct StringBooleanMap: Codable, Hashable, JSONEncodable {


public enum CodingKeys: CodingKey, CaseIterable {
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 Tag: Codable, Hashable {
public struct Tag: Codable, Hashable, JSONEncodable {

public var id: Int64?
public var name: 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 TypeHolderDefault: Codable, Hashable {
public struct TypeHolderDefault: Codable, Hashable, JSONEncodable {

public var stringItem: String = "what"
public var numberItem: Double
Expand Down
Loading