Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[azure-spring-data-cosmos] Turn java.math.Big{Decimal,Integer} into simple types #40239

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions sdk/spring/azure-spring-data-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Bugs Fixed
* Fixing bug with annotated queries that have no where clause but do have a sort - See [PR 40083](https://github.com/Azure/azure-sdk-for-java/pull/40083).
* Fixing bug with Spring JPA keywords that don't lead to criteria creation - See [PR 40167](https://github.com/Azure/azure-sdk-for-java/pull/40167).
* Fixing bug that `java.math.BigInteger` and `java.math.BigDecimal` in models can cause reflection errors on Java 17 - See [PR 40239](https://github.com/Azure/azure-sdk-for-java/pull/40239)

#### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import org.springframework.data.mapping.model.SimpleTypeHolder;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -20,6 +22,8 @@ final class CosmosSimpleTypes {
static {
Set<Class<?>> simpleTypes = new HashSet<>();
simpleTypes.add(UUID.class);
simpleTypes.add(BigDecimal.class);
simpleTypes.add(BigInteger.class);

COSMOS_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.azure.cosmos.models.IndexingMode;
import com.azure.spring.data.cosmos.domain.Address;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -120,6 +122,12 @@ public final class TestConstants {
public static final String PROPERTY_MESSAGE = "message";
public static final String PROPERTY_DATE = "date";

public static final String PROPERTY_BIG_INTEGER = "bigInteger";
public static final String PROPERTY_BIG_DECIMAL = "bigDecimal";

public static final BigInteger BIG_INTEGER = new BigInteger("10000000000000000");
public static final BigDecimal BIG_DECIMAL = new BigDecimal("12345678.12345678");

public static final String PROPERTY_ETAG_DEFAULT = "_etag";
public static final String PROPERTY_ETAG_RENAMED = "etag";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.azure.spring.data.cosmos.core.convert.ObjectMapperFactory;
import com.azure.spring.data.cosmos.core.mapping.CosmosMappingContext;
import com.azure.spring.data.cosmos.domain.Address;
import com.azure.spring.data.cosmos.domain.BigJavaMathTypes;
import com.azure.spring.data.cosmos.domain.Importance;
import com.azure.spring.data.cosmos.domain.Memo;
import com.azure.spring.data.cosmos.domain.Person;
Expand Down Expand Up @@ -100,6 +101,29 @@ public void canReadPojoWithDateFromDocument() throws ParseException {
assertThat(memo.getDate().getTime()).isEqualTo(date);
}

@Test
public void canWritePojoWithBigJavaMathTypes() {
final BigJavaMathTypes javaMathTypes = new BigJavaMathTypes(TestConstants.ID_1, TestConstants.BIG_DECIMAL, TestConstants.BIG_INTEGER);
final JsonNode jsonNode = mappingCosmosConverter.writeJsonNode(javaMathTypes);

assertThat(jsonNode.get(TestConstants.PROPERTY_ID).asText()).isEqualTo(javaMathTypes.getId());
assertThat(jsonNode.get(TestConstants.PROPERTY_BIG_DECIMAL).decimalValue()).isEqualTo(javaMathTypes.getBigDecimal());
assertThat(jsonNode.get(TestConstants.PROPERTY_BIG_INTEGER).bigIntegerValue()).isEqualTo(javaMathTypes.getBigInteger());
}

@Test
public void canReadPojoWithBigJavaMathTypes() {
final ObjectNode jsonObject = ObjectMapperFactory.getObjectMapper().createObjectNode();
jsonObject.put(TestConstants.PROPERTY_ID, TestConstants.ID_1);
jsonObject.put(TestConstants.PROPERTY_BIG_DECIMAL, TestConstants.BIG_DECIMAL);
jsonObject.put(TestConstants.PROPERTY_BIG_INTEGER, TestConstants.BIG_INTEGER);

final BigJavaMathTypes memo = mappingCosmosConverter.read(BigJavaMathTypes.class, jsonObject);
assertThat(memo.getId()).isEqualTo(TestConstants.ID_1);
assertThat(memo.getBigDecimal()).isEqualTo(TestConstants.BIG_DECIMAL);
assertThat(memo.getBigInteger()).isEqualTo(TestConstants.BIG_INTEGER);
}

@Test
public void convertDateValueToMilliSeconds() throws ParseException {
final Date date = TIMEZONE_DATE.parse(TestConstants.DATE_TIMEZONE_STRING);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.azure.spring.data.cosmos.domain;

import com.azure.spring.data.cosmos.common.TestConstants;
import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;
import org.springframework.data.annotation.Id;

import java.util.Objects;

@Container(ru = TestConstants.DEFAULT_MINIMUM_RU)
public class BigJavaMathTypes {
@Id
@PartitionKey
private String id;
private java.math.BigDecimal bigDecimal;
private java.math.BigInteger bigInteger;

public BigJavaMathTypes(String id, java.math.BigDecimal bigDecimal, java.math.BigInteger bigInteger) {
this.id = id;
this.bigDecimal = bigDecimal;
this.bigInteger = bigInteger;
}

public BigJavaMathTypes() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public java.math.BigDecimal getBigDecimal() {
return bigDecimal;
}

public void setBigDecimal(java.math.BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}

public java.math.BigInteger getBigInteger() {
return bigInteger;
}

public void setBigInteger(java.math.BigInteger bigInteger) {
this.bigInteger = bigInteger;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BigJavaMathTypes that = (BigJavaMathTypes) o;
return Objects.equals(id, that.id) && Objects.equals(bigDecimal, that.bigDecimal) && Objects.equals(bigInteger, that.bigInteger);
}

@Override
public int hashCode() {
return Objects.hash(id, bigDecimal, bigInteger);
}

@Override
public String toString() {
return "BigJavaMathTypes{" +
"id='" + id + '\'' +
", bigDecimal=" + bigDecimal +
", bigInteger=" + bigInteger +
'}';
}
}
Loading