typelizer 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82ca16632db02541212afb1fa0cd9b47bbaa10a375b68c4d830e4264eeec5fd8
4
- data.tar.gz: c12d5e0a2aea772483d92b8290dcfa9c6a5e5e38febebf3d986db126f00e4d87
3
+ metadata.gz: 505210c62639d70e95336cfb02bb0ab952089ac8433d5816774977853af264a1
4
+ data.tar.gz: 2878868d86acf4da05caaf5e139e3ccacf58025b8bf5fc2bc56c04b0a91049b4
5
5
  SHA512:
6
- metadata.gz: 5e04629e8669b5ed6c610c85246ea06d3bda46100d2d9d876ad2d0ae6904669aeb569b689b4634df39217a392028bb968060f30a7b4f1f6a47c6374c0604d84d
7
- data.tar.gz: ba808f9f614f856fcd6f3c496b990bbb1ead4530e0214d3d55cbadd62a63e5a9846623ff446d006570d5b4c2b499bfe7bb46c9049c95dc74caf9df81e7a80187
6
+ metadata.gz: b1522f196e8372d77e253bba7f9515043f925aa0f4d8ac6c3b1a11b837424743bba3225774b9dbd65232e46befdabbcb440ef6cf3370edc61e9dece9bf204e2e
7
+ data.tar.gz: 6e8acdb494ec3f5167bbfe9f53422ebd26d4e9d8dab89ec832518b1acda47aae821a002dd954daa7605f273f323092e3622699dffad88805120dbf3d3ad2fa4e
data/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning].
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.9.2] - 2026-02-26
11
+
12
+ ### Fixed
13
+
14
+ - Fix string literal unions in OpenAPI and bracket-aware union type splitting. ([@skryukov])
15
+
10
16
  ## [0.9.1] - 2026-02-26
11
17
 
12
18
  ### Fixed
@@ -420,7 +426,8 @@ and this project adheres to [Semantic Versioning].
420
426
  [@skryukov]: https://github.com/skryukov
421
427
  [@ventsislaf]: https://github.com/ventsislaf
422
428
 
423
- [Unreleased]: https://github.com/skryukov/typelizer/compare/v0.9.1...HEAD
429
+ [Unreleased]: https://github.com/skryukov/typelizer/compare/v0.9.2...HEAD
430
+ [0.9.2]: https://github.com/skryukov/typelizer/compare/v0.9.1...v0.9.2
424
431
  [0.9.1]: https://github.com/skryukov/typelizer/compare/v0.9.0...v0.9.1
425
432
  [0.9.0]: https://github.com/skryukov/typelizer/compare/v0.8.0...v0.9.0
426
433
  [0.8.0]: https://github.com/skryukov/typelizer/compare/v0.7.0...v0.8.0
@@ -89,9 +89,11 @@ module Typelizer
89
89
  end
90
90
 
91
91
  def union_schema(property, openapi_version:)
92
- schemas = property.type.map { |part| union_member_schema(part) }
93
-
94
- definition = {anyOf: schemas}
92
+ definition = if property.type.all? { |part| string_literal?(part) }
93
+ {type: :string, enum: property.type.map { |part| unquote_string_literal(part) }}
94
+ else
95
+ {anyOf: property.type.map { |part| union_member_schema(part) }}
96
+ end
95
97
 
96
98
  unless property.multi
97
99
  apply_nullable(definition, property, openapi_version: openapi_version)
@@ -154,6 +156,8 @@ module Typelizer
154
156
  elsif definition[:type]
155
157
  v31?(openapi_version) ? definition[:type] = [definition[:type], :null] : definition[:nullable] = true
156
158
  end
159
+
160
+ definition[:enum] << nil if definition[:enum].is_a?(Array) && !definition[:enum].include?(nil)
157
161
  end
158
162
 
159
163
  def wrap_multi(definition, property, openapi_version:)
@@ -180,6 +184,8 @@ module Typelizer
180
184
  result = COLUMN_TYPE_MAP[property.column_type].dup
181
185
  result[:type] = :string if property.enum
182
186
  result
187
+ elsif string_literal?(property.type)
188
+ {type: :string, enum: [unquote_string_literal(property.type)]}
183
189
  elsif (property.type.is_a?(String) || property.type.is_a?(Symbol)) && !OPENAPI_TYPES.include?(property.type.to_sym) && !ts_only_type?(property.type.to_s)
184
190
  {"$ref" => "#/components/schemas/#{property.type}"}
185
191
  else
@@ -206,6 +212,16 @@ module Typelizer
206
212
  type_str.start_with?("{", "[") || type_str.include?("<") || TS_OBJECT_TYPES.include?(type_str)
207
213
  end
208
214
 
215
+ def string_literal?(type)
216
+ str = type.to_s
217
+ str.length > 2 &&
218
+ ((str.start_with?("'") && str.end_with?("'")) || (str.start_with?('"') && str.end_with?('"')))
219
+ end
220
+
221
+ def unquote_string_literal(type)
222
+ type.to_s[1..-2]
223
+ end
224
+
209
225
  def validate_version!(openapi_version)
210
226
  raise ArgumentError, "Unsupported openapi_version: #{openapi_version}. Must be one of: #{SUPPORTED_VERSIONS.join(", ")}" unless SUPPORTED_VERSIONS.include?(openapi_version.to_s)
211
227
  end
@@ -40,7 +40,11 @@ module Typelizer
40
40
  private
41
41
 
42
42
  def parse_union(type_str, **options)
43
- parts = type_str.split(/\s*\|\s*/)
43
+ parts = UnionTypeSorter.split_union_members(type_str)
44
+
45
+ # No top-level | found — the | is nested inside brackets
46
+ return {type: type_str.to_sym}.merge(options) if parts.size <= 1
47
+
44
48
  options[:nullable] = true if parts.delete("null")
45
49
  if parts.size == 1
46
50
  parse(parts.first, **options)
@@ -98,10 +98,10 @@ module Typelizer
98
98
 
99
99
  str.each_char do |char|
100
100
  case char
101
- when "<", "("
101
+ when "<", "(", "{", "["
102
102
  depth += 1
103
103
  current << char
104
- when ">", ")"
104
+ when ">", ")", "}", "]"
105
105
  depth -= 1
106
106
  current << char
107
107
  when "|"
@@ -126,6 +126,8 @@ module Typelizer
126
126
  def self.balanced_brackets?(str)
127
127
  angle_depth = 0
128
128
  paren_depth = 0
129
+ brace_depth = 0
130
+ bracket_depth = 0
129
131
 
130
132
  str.each_char do |char|
131
133
  case char
@@ -139,10 +141,20 @@ module Typelizer
139
141
  when ")"
140
142
  paren_depth -= 1
141
143
  return false if paren_depth < 0
144
+ when "{"
145
+ brace_depth += 1
146
+ when "}"
147
+ brace_depth -= 1
148
+ return false if brace_depth < 0
149
+ when "["
150
+ bracket_depth += 1
151
+ when "]"
152
+ bracket_depth -= 1
153
+ return false if bracket_depth < 0
142
154
  end
143
155
  end
144
156
 
145
- angle_depth == 0 && paren_depth == 0
157
+ angle_depth == 0 && paren_depth == 0 && brace_depth == 0 && bracket_depth == 0
146
158
  end
147
159
  end
148
160
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Typelizer
4
- VERSION = "0.9.1"
4
+ VERSION = "0.9.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typelizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Svyatoslav Kryukov