From 1751aa72723cced22833c4484e16be1d43a34456 Mon Sep 17 00:00:00 2001 From: Yusuke Hosonuma Date: Mon, 10 May 2021 17:17:49 +0900 Subject: [PATCH] [crystal][client] some fix and minor improvement (#9416) * [crystal][client] remove redundant include JSON::Serializable * [crystal][client] fix syntax error * [crystal][client] fix shard name * [crystal][client] add 'require "json"' * [crystal][client] update sample projects --- .../resources/crystal/configuration.mustache | 60 +++++++++---------- .../src/main/resources/crystal/model.mustache | 1 + .../crystal/partial_model_generic.mustache | 6 +- .../src/main/resources/crystal/shard.mustache | 2 +- samples/client/petstore/crystal/shard.yml | 2 +- .../crystal/src/petstore/configuration.cr | 26 ++++---- .../src/petstore/models/api_response.cr | 8 +-- .../crystal/src/petstore/models/category.cr | 7 ++- .../crystal/src/petstore/models/order.cr | 11 ++-- .../crystal/src/petstore/models/pet.cr | 11 ++-- .../crystal/src/petstore/models/tag.cr | 7 ++- .../crystal/src/petstore/models/user.cr | 13 ++-- 12 files changed, 74 insertions(+), 80 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/crystal/configuration.mustache b/modules/openapi-generator/src/main/resources/crystal/configuration.mustache index 7111ba75e75..cc2e15e93fa 100644 --- a/modules/openapi-generator/src/main/resources/crystal/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/crystal/configuration.mustache @@ -197,44 +197,44 @@ module {{moduleName}} # Returns Auth Settings hash for api client. def auth_settings - Hash{ {{#authMethods}}{{#isApiKey}}"{{name}}" => { - type: "api_key", - in: {{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}, - key: "{{keyParamName}}", - value: api_key_with_prefix("{{keyParamName}}") - }, + Hash{ +{{#authMethods}} +{{#isApiKey}} + "{{name}}" => { + type: "api_key", + in: {{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}, + key: "{{keyParamName}}", + value: api_key_with_prefix("{{keyParamName}}") + }, {{/isApiKey}} {{#isBasic}} {{#isBasicBasic}} - "{{name}}" => - { - type: "basic", - in: "header", - key: "Authorization", - value: basic_auth_token - }, + "{{name}}" => { + type: "basic", + in: "header", + key: "Authorization", + value: basic_auth_token + }, {{/isBasicBasic}} {{#isBasicBearer}} - "{{name}}" => - { - type: "bearer", - in: "header", - {{#bearerFormat}} - format: "{{{.}}}", - {{/bearerFormat}} - key: "Authorization", - value: "Bearer #{access_token}" - }, + "{{name}}" => { + type: "bearer", + in: "header", + {{#bearerFormat}} + format: "{{{.}}}", + {{/bearerFormat}} + key: "Authorization", + value: "Bearer #{access_token}" + }, {{/isBasicBearer}} {{/isBasic}} {{#isOAuth}} - "{{name}}" => - { - type: "oauth2", - in: "header", - key: "Authorization", - value: "Bearer #{access_token}" - }, + "{{name}}" => { + type: "oauth2", + in: "header", + key: "Authorization", + value: "Bearer #{access_token}" + }, {{/isOAuth}} {{/authMethods}} } diff --git a/modules/openapi-generator/src/main/resources/crystal/model.mustache b/modules/openapi-generator/src/main/resources/crystal/model.mustache index a19cc5f3f6a..021857ee40e 100644 --- a/modules/openapi-generator/src/main/resources/crystal/model.mustache +++ b/modules/openapi-generator/src/main/resources/crystal/model.mustache @@ -1,5 +1,6 @@ # {{#lambdaPrefixWithHash}}{{> api_info}}{{/lambdaPrefixWithHash}} +require "json" require "time" module {{moduleName}} diff --git a/modules/openapi-generator/src/main/resources/crystal/partial_model_generic.mustache b/modules/openapi-generator/src/main/resources/crystal/partial_model_generic.mustache index c36181c48dc..eaf47753fba 100644 --- a/modules/openapi-generator/src/main/resources/crystal/partial_model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/crystal/partial_model_generic.mustache @@ -1,8 +1,10 @@ {{#description}} # {{{description}}} {{/description}} - class {{classname}}{{#parent}} < {{{.}}}{{/parent}} include JSON::Serializable - include JSON::Serializable {{#vars}} + class {{classname}}{{#parent}} < {{{.}}}{{/parent}} + include JSON::Serializable + + {{#vars}} {{#description}} # {{{description}}} {{/description}} diff --git a/modules/openapi-generator/src/main/resources/crystal/shard.mustache b/modules/openapi-generator/src/main/resources/crystal/shard.mustache index 89376ca4593..11b01286538 100644 --- a/modules/openapi-generator/src/main/resources/crystal/shard.mustache +++ b/modules/openapi-generator/src/main/resources/crystal/shard.mustache @@ -1,4 +1,4 @@ -name: {{{moduleName}}} +name: {{{shardName}}} version: {{{shardVersion}}} authors: - {{{shardAuthors}}} diff --git a/samples/client/petstore/crystal/shard.yml b/samples/client/petstore/crystal/shard.yml index 06fc3300e05..a4c86a06e2a 100644 --- a/samples/client/petstore/crystal/shard.yml +++ b/samples/client/petstore/crystal/shard.yml @@ -1,4 +1,4 @@ -name: Petstore +name: petstore version: 1.0.0 authors: - diff --git a/samples/client/petstore/crystal/src/petstore/configuration.cr b/samples/client/petstore/crystal/src/petstore/configuration.cr index 78022839159..e02acd2e507 100644 --- a/samples/client/petstore/crystal/src/petstore/configuration.cr +++ b/samples/client/petstore/crystal/src/petstore/configuration.cr @@ -205,19 +205,19 @@ module Petstore # Returns Auth Settings hash for api client. def auth_settings - Hash{ "api_key" => { - type: "api_key", - in: "header", - key: "api_key", - value: api_key_with_prefix("api_key") - }, - "petstore_auth" => - { - type: "oauth2", - in: "header", - key: "Authorization", - value: "Bearer #{access_token}" - }, + Hash{ + "api_key" => { + type: "api_key", + in: "header", + key: "api_key", + value: api_key_with_prefix("api_key") + }, + "petstore_auth" => { + type: "oauth2", + in: "header", + key: "Authorization", + value: "Bearer #{access_token}" + }, } end diff --git a/samples/client/petstore/crystal/src/petstore/models/api_response.cr b/samples/client/petstore/crystal/src/petstore/models/api_response.cr index b0603e98c03..7d15b7200f8 100644 --- a/samples/client/petstore/crystal/src/petstore/models/api_response.cr +++ b/samples/client/petstore/crystal/src/petstore/models/api_response.cr @@ -8,20 +8,20 @@ #OpenAPI Generator version: 5.2.0-SNAPSHOT # +require "json" require "time" module Petstore # Describes the result of uploading an image resource - class ApiResponse include JSON::Serializable - include JSON::Serializable + class ApiResponse + include JSON::Serializable + @[JSON::Field(key: code, type: Int32)] property code : Int32 - @[JSON::Field(key: type, type: String)] property _type : String - @[JSON::Field(key: message, type: String)] property message : String diff --git a/samples/client/petstore/crystal/src/petstore/models/category.cr b/samples/client/petstore/crystal/src/petstore/models/category.cr index d96bac1cf1c..7d573eb7a82 100644 --- a/samples/client/petstore/crystal/src/petstore/models/category.cr +++ b/samples/client/petstore/crystal/src/petstore/models/category.cr @@ -8,16 +8,17 @@ #OpenAPI Generator version: 5.2.0-SNAPSHOT # +require "json" require "time" module Petstore # A category for a pet - class Category include JSON::Serializable - include JSON::Serializable + class Category + include JSON::Serializable + @[JSON::Field(key: id, type: Int64)] property id : Int64 - @[JSON::Field(key: name, type: String)] property name : String diff --git a/samples/client/petstore/crystal/src/petstore/models/order.cr b/samples/client/petstore/crystal/src/petstore/models/order.cr index ee5a4356492..05de437e07c 100644 --- a/samples/client/petstore/crystal/src/petstore/models/order.cr +++ b/samples/client/petstore/crystal/src/petstore/models/order.cr @@ -8,33 +8,30 @@ #OpenAPI Generator version: 5.2.0-SNAPSHOT # +require "json" require "time" module Petstore # An order for a pets from the pet store - class Order include JSON::Serializable - include JSON::Serializable + class Order + include JSON::Serializable + @[JSON::Field(key: id, type: Int64)] property id : Int64 - @[JSON::Field(key: petId, type: Int64)] property pet_id : Int64 - @[JSON::Field(key: quantity, type: Int32)] property quantity : Int32 - @[JSON::Field(key: shipDate, type: Time)] property ship_date : Time - # Order Status @[JSON::Field(key: status, type: String)] property status : String - @[JSON::Field(key: complete, type: Bool)] property complete : Bool diff --git a/samples/client/petstore/crystal/src/petstore/models/pet.cr b/samples/client/petstore/crystal/src/petstore/models/pet.cr index 91e5552b02e..2d2c236e48b 100644 --- a/samples/client/petstore/crystal/src/petstore/models/pet.cr +++ b/samples/client/petstore/crystal/src/petstore/models/pet.cr @@ -8,32 +8,29 @@ #OpenAPI Generator version: 5.2.0-SNAPSHOT # +require "json" require "time" module Petstore # A pet for sale in the pet store - class Pet include JSON::Serializable - include JSON::Serializable + class Pet + include JSON::Serializable + @[JSON::Field(key: id, type: Int64)] property id : Int64 - @[JSON::Field(key: category, type: Category)] property category : Category - @[JSON::Field(key: name, type: String)] property name : String - @[JSON::Field(key: photoUrls, type: Array(String))] property photo_urls : Array(String) - @[JSON::Field(key: tags, type: Array(Tag))] property tags : Array(Tag) - # pet status in the store @[JSON::Field(key: status, type: String)] property status : String diff --git a/samples/client/petstore/crystal/src/petstore/models/tag.cr b/samples/client/petstore/crystal/src/petstore/models/tag.cr index 4121f2e67a7..6e6f16a21d3 100644 --- a/samples/client/petstore/crystal/src/petstore/models/tag.cr +++ b/samples/client/petstore/crystal/src/petstore/models/tag.cr @@ -8,16 +8,17 @@ #OpenAPI Generator version: 5.2.0-SNAPSHOT # +require "json" require "time" module Petstore # A tag for a pet - class Tag include JSON::Serializable - include JSON::Serializable + class Tag + include JSON::Serializable + @[JSON::Field(key: id, type: Int64)] property id : Int64 - @[JSON::Field(key: name, type: String)] property name : String diff --git a/samples/client/petstore/crystal/src/petstore/models/user.cr b/samples/client/petstore/crystal/src/petstore/models/user.cr index 4f0d617e98d..3732dc4d2e4 100644 --- a/samples/client/petstore/crystal/src/petstore/models/user.cr +++ b/samples/client/petstore/crystal/src/petstore/models/user.cr @@ -8,40 +8,35 @@ #OpenAPI Generator version: 5.2.0-SNAPSHOT # +require "json" require "time" module Petstore # A User who is purchasing from the pet store - class User include JSON::Serializable - include JSON::Serializable + class User + include JSON::Serializable + @[JSON::Field(key: id, type: Int64)] property id : Int64 - @[JSON::Field(key: username, type: String)] property username : String - @[JSON::Field(key: firstName, type: String)] property first_name : String - @[JSON::Field(key: lastName, type: String)] property last_name : String - @[JSON::Field(key: email, type: String)] property email : String - @[JSON::Field(key: password, type: String)] property password : String - @[JSON::Field(key: phone, type: String)] property phone : String - # User Status @[JSON::Field(key: userStatus, type: Int32)] property user_status : Int32