Skip to content

Commit 3d1f4ad

Browse files
refactor(internal): use constructor to deserialize json (#66)
1 parent 52aab03 commit 3d1f4ad

File tree

76 files changed

+1476
-1678
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1476
-1678
lines changed

openai-java-core/src/main/kotlin/com/openai/core/Utils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ internal fun <T> List<T>.toImmutable(): List<T> =
1616

1717
@JvmSynthetic
1818
internal fun <K, V> Map<K, V>.toImmutable(): Map<K, V> =
19-
if (isEmpty()) Collections.emptyMap() else Collections.unmodifiableMap(toMap())
19+
if (isEmpty()) immutableEmptyMap() else Collections.unmodifiableMap(toMap())
20+
21+
@JvmSynthetic internal fun <K, V> immutableEmptyMap(): Map<K, V> = Collections.emptyMap()
2022

2123
@JvmSynthetic
2224
internal fun <K : Comparable<K>, V> SortedMap<K, V>.toImmutable(): SortedMap<K, V> =

openai-java-core/src/main/kotlin/com/openai/errors/OpenAIError.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ package com.openai.errors
44

55
import com.fasterxml.jackson.annotation.JsonAnyGetter
66
import com.fasterxml.jackson.annotation.JsonAnySetter
7-
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
7+
import com.fasterxml.jackson.annotation.JsonCreator
88
import com.openai.core.ExcludeMissing
99
import com.openai.core.JsonValue
1010
import com.openai.core.NoAutoDetect
11+
import com.openai.core.immutableEmptyMap
1112
import com.openai.core.toImmutable
1213
import java.util.Objects
1314

14-
@JsonDeserialize(builder = OpenAIError.Builder::class)
1515
@NoAutoDetect
1616
class OpenAIError
17+
@JsonCreator
1718
private constructor(
1819
@JsonAnyGetter
1920
@ExcludeMissing
21+
@JsonAnySetter
2022
@get:JvmName("additionalProperties")
21-
val additionalProperties: Map<String, JsonValue>,
23+
val additionalProperties: Map<String, JsonValue> = immutableEmptyMap(),
2224
) {
2325

2426
fun toBuilder() = Builder().from(this)
@@ -42,7 +44,6 @@ private constructor(
4244
putAllAdditionalProperties(additionalProperties)
4345
}
4446

45-
@JsonAnySetter
4647
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
4748
additionalProperties.put(key, value)
4849
}

openai-java-core/src/main/kotlin/com/openai/models/Batch.kt

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,79 @@ import com.fasterxml.jackson.annotation.JsonAnyGetter
66
import com.fasterxml.jackson.annotation.JsonAnySetter
77
import com.fasterxml.jackson.annotation.JsonCreator
88
import com.fasterxml.jackson.annotation.JsonProperty
9-
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
109
import com.openai.core.Enum
1110
import com.openai.core.ExcludeMissing
1211
import com.openai.core.JsonField
1312
import com.openai.core.JsonMissing
1413
import com.openai.core.JsonValue
1514
import com.openai.core.NoAutoDetect
15+
import com.openai.core.immutableEmptyMap
1616
import com.openai.core.toImmutable
1717
import com.openai.errors.OpenAIInvalidDataException
1818
import java.util.Objects
1919
import java.util.Optional
2020

21-
@JsonDeserialize(builder = Batch.Builder::class)
2221
@NoAutoDetect
2322
class Batch
23+
@JsonCreator
2424
private constructor(
25-
private val id: JsonField<String>,
26-
private val object_: JsonField<Object>,
27-
private val endpoint: JsonField<String>,
28-
private val errors: JsonField<Errors>,
29-
private val inputFileId: JsonField<String>,
30-
private val completionWindow: JsonField<String>,
31-
private val status: JsonField<Status>,
32-
private val outputFileId: JsonField<String>,
33-
private val errorFileId: JsonField<String>,
34-
private val createdAt: JsonField<Long>,
35-
private val inProgressAt: JsonField<Long>,
36-
private val expiresAt: JsonField<Long>,
37-
private val finalizingAt: JsonField<Long>,
38-
private val completedAt: JsonField<Long>,
39-
private val failedAt: JsonField<Long>,
40-
private val expiredAt: JsonField<Long>,
41-
private val cancellingAt: JsonField<Long>,
42-
private val cancelledAt: JsonField<Long>,
43-
private val requestCounts: JsonField<BatchRequestCounts>,
44-
private val metadata: JsonValue,
45-
private val additionalProperties: Map<String, JsonValue>,
25+
@JsonProperty("id") @ExcludeMissing private val id: JsonField<String> = JsonMissing.of(),
26+
@JsonProperty("object")
27+
@ExcludeMissing
28+
private val object_: JsonField<Object> = JsonMissing.of(),
29+
@JsonProperty("endpoint")
30+
@ExcludeMissing
31+
private val endpoint: JsonField<String> = JsonMissing.of(),
32+
@JsonProperty("errors")
33+
@ExcludeMissing
34+
private val errors: JsonField<Errors> = JsonMissing.of(),
35+
@JsonProperty("input_file_id")
36+
@ExcludeMissing
37+
private val inputFileId: JsonField<String> = JsonMissing.of(),
38+
@JsonProperty("completion_window")
39+
@ExcludeMissing
40+
private val completionWindow: JsonField<String> = JsonMissing.of(),
41+
@JsonProperty("status")
42+
@ExcludeMissing
43+
private val status: JsonField<Status> = JsonMissing.of(),
44+
@JsonProperty("output_file_id")
45+
@ExcludeMissing
46+
private val outputFileId: JsonField<String> = JsonMissing.of(),
47+
@JsonProperty("error_file_id")
48+
@ExcludeMissing
49+
private val errorFileId: JsonField<String> = JsonMissing.of(),
50+
@JsonProperty("created_at")
51+
@ExcludeMissing
52+
private val createdAt: JsonField<Long> = JsonMissing.of(),
53+
@JsonProperty("in_progress_at")
54+
@ExcludeMissing
55+
private val inProgressAt: JsonField<Long> = JsonMissing.of(),
56+
@JsonProperty("expires_at")
57+
@ExcludeMissing
58+
private val expiresAt: JsonField<Long> = JsonMissing.of(),
59+
@JsonProperty("finalizing_at")
60+
@ExcludeMissing
61+
private val finalizingAt: JsonField<Long> = JsonMissing.of(),
62+
@JsonProperty("completed_at")
63+
@ExcludeMissing
64+
private val completedAt: JsonField<Long> = JsonMissing.of(),
65+
@JsonProperty("failed_at")
66+
@ExcludeMissing
67+
private val failedAt: JsonField<Long> = JsonMissing.of(),
68+
@JsonProperty("expired_at")
69+
@ExcludeMissing
70+
private val expiredAt: JsonField<Long> = JsonMissing.of(),
71+
@JsonProperty("cancelling_at")
72+
@ExcludeMissing
73+
private val cancellingAt: JsonField<Long> = JsonMissing.of(),
74+
@JsonProperty("cancelled_at")
75+
@ExcludeMissing
76+
private val cancelledAt: JsonField<Long> = JsonMissing.of(),
77+
@JsonProperty("request_counts")
78+
@ExcludeMissing
79+
private val requestCounts: JsonField<BatchRequestCounts> = JsonMissing.of(),
80+
@JsonProperty("metadata") @ExcludeMissing private val metadata: JsonValue = JsonMissing.of(),
81+
@JsonAnySetter private val additionalProperties: Map<String, JsonValue> = immutableEmptyMap(),
4682
) {
4783

4884
fun id(): String = id.getRequired("id")
@@ -257,45 +293,35 @@ private constructor(
257293

258294
fun id(id: String) = id(JsonField.of(id))
259295

260-
@JsonProperty("id") @ExcludeMissing fun id(id: JsonField<String>) = apply { this.id = id }
296+
fun id(id: JsonField<String>) = apply { this.id = id }
261297

262298
/** The object type, which is always `batch`. */
263299
fun object_(object_: Object) = object_(JsonField.of(object_))
264300

265301
/** The object type, which is always `batch`. */
266-
@JsonProperty("object")
267-
@ExcludeMissing
268302
fun object_(object_: JsonField<Object>) = apply { this.object_ = object_ }
269303

270304
/** The OpenAI API endpoint used by the batch. */
271305
fun endpoint(endpoint: String) = endpoint(JsonField.of(endpoint))
272306

273307
/** The OpenAI API endpoint used by the batch. */
274-
@JsonProperty("endpoint")
275-
@ExcludeMissing
276308
fun endpoint(endpoint: JsonField<String>) = apply { this.endpoint = endpoint }
277309

278310
fun errors(errors: Errors) = errors(JsonField.of(errors))
279311

280-
@JsonProperty("errors")
281-
@ExcludeMissing
282312
fun errors(errors: JsonField<Errors>) = apply { this.errors = errors }
283313

284314
/** The ID of the input file for the batch. */
285315
fun inputFileId(inputFileId: String) = inputFileId(JsonField.of(inputFileId))
286316

287317
/** The ID of the input file for the batch. */
288-
@JsonProperty("input_file_id")
289-
@ExcludeMissing
290318
fun inputFileId(inputFileId: JsonField<String>) = apply { this.inputFileId = inputFileId }
291319

292320
/** The time frame within which the batch should be processed. */
293321
fun completionWindow(completionWindow: String) =
294322
completionWindow(JsonField.of(completionWindow))
295323

296324
/** The time frame within which the batch should be processed. */
297-
@JsonProperty("completion_window")
298-
@ExcludeMissing
299325
fun completionWindow(completionWindow: JsonField<String>) = apply {
300326
this.completionWindow = completionWindow
301327
}
@@ -304,16 +330,12 @@ private constructor(
304330
fun status(status: Status) = status(JsonField.of(status))
305331

306332
/** The current status of the batch. */
307-
@JsonProperty("status")
308-
@ExcludeMissing
309333
fun status(status: JsonField<Status>) = apply { this.status = status }
310334

311335
/** The ID of the file containing the outputs of successfully executed requests. */
312336
fun outputFileId(outputFileId: String) = outputFileId(JsonField.of(outputFileId))
313337

314338
/** The ID of the file containing the outputs of successfully executed requests. */
315-
@JsonProperty("output_file_id")
316-
@ExcludeMissing
317339
fun outputFileId(outputFileId: JsonField<String>) = apply {
318340
this.outputFileId = outputFileId
319341
}
@@ -322,89 +344,67 @@ private constructor(
322344
fun errorFileId(errorFileId: String) = errorFileId(JsonField.of(errorFileId))
323345

324346
/** The ID of the file containing the outputs of requests with errors. */
325-
@JsonProperty("error_file_id")
326-
@ExcludeMissing
327347
fun errorFileId(errorFileId: JsonField<String>) = apply { this.errorFileId = errorFileId }
328348

329349
/** The Unix timestamp (in seconds) for when the batch was created. */
330350
fun createdAt(createdAt: Long) = createdAt(JsonField.of(createdAt))
331351

332352
/** The Unix timestamp (in seconds) for when the batch was created. */
333-
@JsonProperty("created_at")
334-
@ExcludeMissing
335353
fun createdAt(createdAt: JsonField<Long>) = apply { this.createdAt = createdAt }
336354

337355
/** The Unix timestamp (in seconds) for when the batch started processing. */
338356
fun inProgressAt(inProgressAt: Long) = inProgressAt(JsonField.of(inProgressAt))
339357

340358
/** The Unix timestamp (in seconds) for when the batch started processing. */
341-
@JsonProperty("in_progress_at")
342-
@ExcludeMissing
343359
fun inProgressAt(inProgressAt: JsonField<Long>) = apply { this.inProgressAt = inProgressAt }
344360

345361
/** The Unix timestamp (in seconds) for when the batch will expire. */
346362
fun expiresAt(expiresAt: Long) = expiresAt(JsonField.of(expiresAt))
347363

348364
/** The Unix timestamp (in seconds) for when the batch will expire. */
349-
@JsonProperty("expires_at")
350-
@ExcludeMissing
351365
fun expiresAt(expiresAt: JsonField<Long>) = apply { this.expiresAt = expiresAt }
352366

353367
/** The Unix timestamp (in seconds) for when the batch started finalizing. */
354368
fun finalizingAt(finalizingAt: Long) = finalizingAt(JsonField.of(finalizingAt))
355369

356370
/** The Unix timestamp (in seconds) for when the batch started finalizing. */
357-
@JsonProperty("finalizing_at")
358-
@ExcludeMissing
359371
fun finalizingAt(finalizingAt: JsonField<Long>) = apply { this.finalizingAt = finalizingAt }
360372

361373
/** The Unix timestamp (in seconds) for when the batch was completed. */
362374
fun completedAt(completedAt: Long) = completedAt(JsonField.of(completedAt))
363375

364376
/** The Unix timestamp (in seconds) for when the batch was completed. */
365-
@JsonProperty("completed_at")
366-
@ExcludeMissing
367377
fun completedAt(completedAt: JsonField<Long>) = apply { this.completedAt = completedAt }
368378

369379
/** The Unix timestamp (in seconds) for when the batch failed. */
370380
fun failedAt(failedAt: Long) = failedAt(JsonField.of(failedAt))
371381

372382
/** The Unix timestamp (in seconds) for when the batch failed. */
373-
@JsonProperty("failed_at")
374-
@ExcludeMissing
375383
fun failedAt(failedAt: JsonField<Long>) = apply { this.failedAt = failedAt }
376384

377385
/** The Unix timestamp (in seconds) for when the batch expired. */
378386
fun expiredAt(expiredAt: Long) = expiredAt(JsonField.of(expiredAt))
379387

380388
/** The Unix timestamp (in seconds) for when the batch expired. */
381-
@JsonProperty("expired_at")
382-
@ExcludeMissing
383389
fun expiredAt(expiredAt: JsonField<Long>) = apply { this.expiredAt = expiredAt }
384390

385391
/** The Unix timestamp (in seconds) for when the batch started cancelling. */
386392
fun cancellingAt(cancellingAt: Long) = cancellingAt(JsonField.of(cancellingAt))
387393

388394
/** The Unix timestamp (in seconds) for when the batch started cancelling. */
389-
@JsonProperty("cancelling_at")
390-
@ExcludeMissing
391395
fun cancellingAt(cancellingAt: JsonField<Long>) = apply { this.cancellingAt = cancellingAt }
392396

393397
/** The Unix timestamp (in seconds) for when the batch was cancelled. */
394398
fun cancelledAt(cancelledAt: Long) = cancelledAt(JsonField.of(cancelledAt))
395399

396400
/** The Unix timestamp (in seconds) for when the batch was cancelled. */
397-
@JsonProperty("cancelled_at")
398-
@ExcludeMissing
399401
fun cancelledAt(cancelledAt: JsonField<Long>) = apply { this.cancelledAt = cancelledAt }
400402

401403
/** The request counts for different statuses within the batch. */
402404
fun requestCounts(requestCounts: BatchRequestCounts) =
403405
requestCounts(JsonField.of(requestCounts))
404406

405407
/** The request counts for different statuses within the batch. */
406-
@JsonProperty("request_counts")
407-
@ExcludeMissing
408408
fun requestCounts(requestCounts: JsonField<BatchRequestCounts>) = apply {
409409
this.requestCounts = requestCounts
410410
}
@@ -414,16 +414,13 @@ private constructor(
414414
* storing additional information about the object in a structured format. Keys can be a
415415
* maximum of 64 characters long and values can be a maximum of 512 characters long.
416416
*/
417-
@JsonProperty("metadata")
418-
@ExcludeMissing
419417
fun metadata(metadata: JsonValue) = apply { this.metadata = metadata }
420418

421419
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
422420
this.additionalProperties.clear()
423421
putAllAdditionalProperties(additionalProperties)
424422
}
425423

426-
@JsonAnySetter
427424
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
428425
additionalProperties.put(key, value)
429426
}
@@ -608,13 +605,18 @@ private constructor(
608605
override fun toString() = value.toString()
609606
}
610607

611-
@JsonDeserialize(builder = Errors.Builder::class)
612608
@NoAutoDetect
613609
class Errors
610+
@JsonCreator
614611
private constructor(
615-
private val object_: JsonField<String>,
616-
private val data: JsonField<List<BatchError>>,
617-
private val additionalProperties: Map<String, JsonValue>,
612+
@JsonProperty("object")
613+
@ExcludeMissing
614+
private val object_: JsonField<String> = JsonMissing.of(),
615+
@JsonProperty("data")
616+
@ExcludeMissing
617+
private val data: JsonField<List<BatchError>> = JsonMissing.of(),
618+
@JsonAnySetter
619+
private val additionalProperties: Map<String, JsonValue> = immutableEmptyMap(),
618620
) {
619621

620622
/** The object type, which is always `list`. */
@@ -665,22 +667,17 @@ private constructor(
665667
fun object_(object_: String) = object_(JsonField.of(object_))
666668

667669
/** The object type, which is always `list`. */
668-
@JsonProperty("object")
669-
@ExcludeMissing
670670
fun object_(object_: JsonField<String>) = apply { this.object_ = object_ }
671671

672672
fun data(data: List<BatchError>) = data(JsonField.of(data))
673673

674-
@JsonProperty("data")
675-
@ExcludeMissing
676674
fun data(data: JsonField<List<BatchError>>) = apply { this.data = data }
677675

678676
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
679677
this.additionalProperties.clear()
680678
putAllAdditionalProperties(additionalProperties)
681679
}
682680

683-
@JsonAnySetter
684681
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
685682
additionalProperties.put(key, value)
686683
}

0 commit comments

Comments
 (0)