model_driven_api 3.1.5 → 3.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c1a8a56c9fefaf12a3a21235edbb91325a78b9d95a63d0cab7ed3ba12cda2bf
4
- data.tar.gz: 50dcc8e5cc20032bb9e912abed38b43c24c24f6917852bc0ccdc139e55dd080c
3
+ metadata.gz: 1272f499542ca137fd53edb4e8e0221021c9640a8d0f964fc41546c6d55e3873
4
+ data.tar.gz: 23c1b8dafdcf82ffdfcc68113568ee07e9bd9ee10efaaeb860be8459e09a1c2e
5
5
  SHA512:
6
- metadata.gz: b140b53e7de7d56349f61713d6b39607132e8bde7c4f15a110f6061f53e76d1d7b4b4460b5ab9c4a5ff107a4dd84dab2c46ad9dba47b2db25b20a756c186c84b
7
- data.tar.gz: 93636d414d491988dbab88b309ac42737c36056637e27e89f8bb36a60b12a9f69fa683ea2d4764b1317ea6963020b68d9422580fbfae5f9277fac24ccfad8b4b
6
+ metadata.gz: 53941a7154e66342de797d2350fffe085f74cc6ddafc75c4d15231e14d1628b1d03c6891ee74674e0a47f634550cfc4c01bb335f2b58b5b592da5b20c4b72559
7
+ data.tar.gz: 22289e5e28cdd414390d2019b9f48526d606eacbdc026c259f3c8444fcf039b712e5099062f0b811970ce3ba00d3803870c97541faa6b94e2c5c2662ee89e856
@@ -84,12 +84,36 @@ class Api::V2::InfoController < Api::V2::ApplicationController
84
84
  method_key.type.to_s
85
85
  end
86
86
 
87
- def create_properties_from_model(model, dsl)
88
- JSON.parse(model.new.to_json(dsl)).keys.map do |k|
89
- # Rails.logger.debug "###################### Model #{model.model_name.human rescue ""} Key #{k}"
87
+ def integer?(str)
88
+ true if Integer(str) rescue false
89
+ end
90
+
91
+ def number?(str)
92
+ true if Float(str) rescue false
93
+ end
94
+
95
+ def datetime?(str)
96
+ true if DateTime.parse(str) rescue false
97
+ end
98
+
99
+ def create_properties_from_model(model, dsl, remove_reserved = false)
100
+ parsed_json = JSON.parse(model.new.to_json(dsl))
101
+ parsed_json.keys.map do |k|
90
102
  type = compute_type(model, k)
91
- # Rails.logger.debug "###################### Model #{model.model_name.human rescue ""} Type for #{k} is #{type}"
92
- if type == "method"
103
+
104
+ # Remove fields that cannot be created or updated
105
+ if remove_reserved && %w( id created_at updated_at lock_version).include?(k.to_s)
106
+ nil
107
+ elsif type == "method" && (parsed_json[k].is_a?(FalseClass) || parsed_json[k].is_a?(TrueClass))
108
+ [k, { "type": "boolean" }]
109
+ elsif type == "method" && parsed_json[k].is_a?(String) && number?(parsed_json[k])
110
+ [k, { "type": "number" }]
111
+ elsif type == "method" && parsed_json[k].is_a?(String) && integer?(parsed_json[k])
112
+ [k, { "type": "integer" }]
113
+ elsif type == "method" && parsed_json[k].is_a?(String) && datetime?(parsed_json[k])
114
+ [k, { "type": "string", "format": "date-time" }]
115
+ elsif type == "method"
116
+ # Unknown or complex format returned
93
117
  [k, { "type": "object", "additionalProperties": true }]
94
118
  elsif type == "date"
95
119
  [k, { "type": "string", "format": "date" }]
@@ -420,7 +444,7 @@ class Api::V2::InfoController < Api::V2::ApplicationController
420
444
  }
421
445
  }
422
446
  }
423
- ApplicationRecord.subclasses.each do |d|
447
+ ApplicationRecord.subclasses.sort_by { |d| d.to_s }.each do |d|
424
448
  # Only if current user can read the model
425
449
  if true # can? :read, d
426
450
  model = d.to_s.underscore.tableize
@@ -468,7 +492,7 @@ class Api::V2::InfoController < Api::V2::ApplicationController
468
492
  "properties": {
469
493
  "#{model.singularize}": {
470
494
  "type": "object",
471
- "properties": create_properties_from_model(d, {})
495
+ "properties": create_properties_from_model(d, {}, true)
472
496
  }
473
497
  }
474
498
  }
@@ -611,7 +635,7 @@ class Api::V2::InfoController < Api::V2::ApplicationController
611
635
  "properties": {
612
636
  "#{model.singularize}": {
613
637
  "type": "object",
614
- "properties": create_properties_from_model(d, {})
638
+ "properties": create_properties_from_model(d, {}, true)
615
639
  }
616
640
  }
617
641
  }
@@ -660,7 +684,7 @@ class Api::V2::InfoController < Api::V2::ApplicationController
660
684
  "properties": {
661
685
  "#{model.singularize}": {
662
686
  "type": "object",
663
- "properties": create_properties_from_model(d, {})
687
+ "properties": create_properties_from_model(d, {}, true)
664
688
  }
665
689
  }
666
690
  }
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "3.1.5".freeze
2
+ VERSION = "3.1.7".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_driven_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.5
4
+ version: 3.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni