Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.52.2"
".": "0.53.0"
}
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 103
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/orb%2Forb-95a3d7780935a38e0cf076d4ad2d68bd1a5641bced8398d972db2e92751d364a.yml
openapi_spec_hash: 9ebe818c4ad4f2d9c4e473b5192d7544
config_hash: ec4f1e02d3528e3a93a73e33bca17c2a
config_hash: 3dc5bc1df028fc7301fb2ada9846f038
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## 0.53.0 (2025-04-02)

Full Changelog: [v0.52.2...v0.53.0](https://github.com/orbcorp/orb-java/compare/v0.52.2...v0.53.0)

### Features

* **client:** add enum validation method ([d6fdaa3](https://github.com/orbcorp/orb-java/commit/d6fdaa34309a8c1bcc4f2bd84beaae587f3a3e9d))
* **client:** make union deserialization more robust ([#379](https://github.com/orbcorp/orb-java/issues/379)) ([d6fdaa3](https://github.com/orbcorp/orb-java/commit/d6fdaa34309a8c1bcc4f2bd84beaae587f3a3e9d))


### Chores

* **client:** remove unnecessary json state from some query param classes ([d6fdaa3](https://github.com/orbcorp/orb-java/commit/d6fdaa34309a8c1bcc4f2bd84beaae587f3a3e9d))
* **internal:** add invalid json deserialization tests ([d6fdaa3](https://github.com/orbcorp/orb-java/commit/d6fdaa34309a8c1bcc4f2bd84beaae587f3a3e9d))
* **internal:** add json roundtripping tests ([d6fdaa3](https://github.com/orbcorp/orb-java/commit/d6fdaa34309a8c1bcc4f2bd84beaae587f3a3e9d))
* **internal:** codegen related update ([#381](https://github.com/orbcorp/orb-java/issues/381)) ([1373dc9](https://github.com/orbcorp/orb-java/commit/1373dc9c246351e40eb442e4af9af81c1b8cb881))

## 0.52.2 (2025-04-01)

Full Changelog: [v0.52.1...v0.52.2](https://github.com/orbcorp/orb-java/compare/v0.52.1...v0.52.2)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- x-release-please-start-version -->

[![Maven Central](https://img.shields.io/maven-central/v/com.withorb.api/orb-java)](https://central.sonatype.com/artifact/com.withorb.api/orb-java/0.52.2)
[![Maven Central](https://img.shields.io/maven-central/v/com.withorb.api/orb-java)](https://central.sonatype.com/artifact/com.withorb.api/orb-java/0.53.0)

<!-- x-release-please-end -->

Expand All @@ -19,7 +19,7 @@ The REST API documentation can be found on [docs.withorb.com](https://docs.witho
### Gradle

```kotlin
implementation("com.withorb.api:orb-java:0.52.2")
implementation("com.withorb.api:orb-java:0.53.0")
```

### Maven
Expand All @@ -28,7 +28,7 @@ implementation("com.withorb.api:orb-java:0.52.2")
<dependency>
<groupId>com.withorb.api</groupId>
<artifactId>orb-java</artifactId>
<version>0.52.2</version>
<version>0.53.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
allprojects {
group = "com.withorb.api"
version = "0.52.2" // x-release-please-version
version = "0.53.0" // x-release-please-version
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import com.fasterxml.jackson.databind.BeanProperty
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.withorb.api.errors.OrbInvalidDataException
import kotlin.reflect.KClass

abstract class BaseDeserializer<T : Any>(type: KClass<T>) :
Expand All @@ -30,38 +28,17 @@ abstract class BaseDeserializer<T : Any>(type: KClass<T>) :

protected abstract fun ObjectCodec.deserialize(node: JsonNode): T

protected fun <T> ObjectCodec.deserialize(node: JsonNode, type: TypeReference<T>): T =
protected fun <T> ObjectCodec.tryDeserialize(node: JsonNode, type: TypeReference<T>): T? =
try {
readValue(treeAsTokens(node), type)
} catch (e: Exception) {
throw OrbInvalidDataException("Error deserializing", e)
}

protected fun <T> ObjectCodec.tryDeserialize(
node: JsonNode,
type: TypeReference<T>,
validate: (T) -> Unit = {},
): T? {
return try {
readValue(treeAsTokens(node), type).apply(validate)
} catch (e: JsonMappingException) {
null
} catch (e: RuntimeException) {
null
}
}

protected fun <T> ObjectCodec.tryDeserialize(
node: JsonNode,
type: JavaType,
validate: (T) -> Unit = {},
): T? {
return try {
readValue<T>(treeAsTokens(node), type).apply(validate)
} catch (e: JsonMappingException) {
null
} catch (e: RuntimeException) {
protected fun <T> ObjectCodec.tryDeserialize(node: JsonNode, type: JavaType): T? =
try {
readValue(treeAsTokens(node), type)
} catch (e: Exception) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,40 @@ package com.withorb.api.core

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.cfg.CoercionAction
import com.fasterxml.jackson.databind.cfg.CoercionInputShape
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.type.LogicalType
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.kotlinModule
import java.io.InputStream
import java.time.DateTimeException
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoField

fun jsonMapper(): JsonMapper =
JsonMapper.builder()
.addModule(kotlinModule())
.addModule(Jdk8Module())
.addModule(JavaTimeModule())
.addModule(SimpleModule().addSerializer(InputStreamJsonSerializer))
.addModule(
SimpleModule()
.addSerializer(InputStreamSerializer)
.addDeserializer(LocalDateTime::class.java, LenientLocalDateTimeDeserializer())
)
.withCoercionConfig(LogicalType.Boolean) {
it.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
Expand Down Expand Up @@ -91,7 +105,10 @@ fun jsonMapper(): JsonMapper =
.disable(MapperFeature.AUTO_DETECT_SETTERS)
.build()

private object InputStreamJsonSerializer : BaseSerializer<InputStream>(InputStream::class) {
/** A serializer that serializes [InputStream] to bytes. */
private object InputStreamSerializer : BaseSerializer<InputStream>(InputStream::class) {

private fun readResolve(): Any = InputStreamSerializer

override fun serialize(
value: InputStream?,
Expand All @@ -105,3 +122,46 @@ private object InputStreamJsonSerializer : BaseSerializer<InputStream>(InputStre
}
}
}

/**
* A deserializer that can deserialize [LocalDateTime] from datetimes, dates, and zoned datetimes.
*/
private class LenientLocalDateTimeDeserializer :
StdDeserializer<LocalDateTime>(LocalDateTime::class.java) {

companion object {

private val DATE_TIME_FORMATTERS =
listOf(
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
DateTimeFormatter.ISO_LOCAL_DATE,
DateTimeFormatter.ISO_ZONED_DATE_TIME,
)
}

override fun logicalType(): LogicalType = LogicalType.DateTime

override fun deserialize(p: JsonParser, context: DeserializationContext?): LocalDateTime {
val exceptions = mutableListOf<Exception>()

for (formatter in DATE_TIME_FORMATTERS) {
try {
val temporal = formatter.parse(p.text)

return when {
!temporal.isSupported(ChronoField.HOUR_OF_DAY) ->
LocalDate.from(temporal).atStartOfDay()
!temporal.isSupported(ChronoField.OFFSET_SECONDS) ->
LocalDateTime.from(temporal)
else -> ZonedDateTime.from(temporal).toLocalDateTime()
}
} catch (e: DateTimeException) {
exceptions.add(e)
}
}

throw JsonParseException(p, "Cannot parse `LocalDateTime` from value: ${p.text}").apply {
exceptions.forEach { addSuppressed(it) }
}
}
}
28 changes: 28 additions & 0 deletions orb-java-core/src/main/kotlin/com/withorb/api/core/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ internal fun <K : Comparable<K>, V> SortedMap<K, V>.toImmutable(): SortedMap<K,
if (isEmpty()) Collections.emptySortedMap()
else Collections.unmodifiableSortedMap(toSortedMap(comparator()))

/**
* Returns all elements that yield the largest value for the given function, or an empty list if
* there are zero elements.
*
* This is similar to [Sequence.maxByOrNull] except it returns _all_ elements that yield the largest
* value; not just the first one.
*/
@JvmSynthetic
internal fun <T, R : Comparable<R>> Sequence<T>.allMaxBy(selector: (T) -> R): List<T> {
var maxValue: R? = null
val maxElements = mutableListOf<T>()

val iterator = iterator()
while (iterator.hasNext()) {
val element = iterator.next()
val value = selector(element)
if (maxValue == null || value > maxValue) {
maxValue = value
maxElements.clear()
maxElements.add(element)
} else if (value == maxValue) {
maxElements.add(element)
}
}

return maxElements
}

/**
* Returns whether [this] is equal to [other].
*
Expand Down
Loading