Skip to content

Commit 8f37063

Browse files
http-client-java, generate mock Long number for BigDecimal (#5433)
- mock test data in mgmt was not able to handle `BigDecimal` type Azure/azure-sdk-for-java#43569 - try break the string if too long Azure/azure-sdk-for-java#43574 (but still have "code too large" error, as byte code for a method need to be under 64K)
1 parent 4fd8d1f commit 8f37063

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/javamodel/JavaPackage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.microsoft.typespec.http.client.generator.core.template.Templates;
4040
import com.microsoft.typespec.http.client.generator.core.template.TestProxyAssetsTemplate;
4141
import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil;
42+
import com.microsoft.typespec.http.client.generator.core.util.ConstantStringTooLongException;
4243
import com.microsoft.typespec.http.client.generator.core.util.PossibleCredentialException;
4344
import java.io.BufferedReader;
4445
import java.io.IOException;
@@ -299,6 +300,9 @@ public void addModelUnitTest(ClientModel model) {
299300
} catch (PossibleCredentialException e) {
300301
// skip this test file
301302
logger.warn("Skip unit test for model '{}', caused by key '{}'", model.getName(), e.getKeyName());
303+
} catch (ConstantStringTooLongException e) {
304+
// skip this test file
305+
logger.warn("Skip unit test for model '{}', JSON string is too long.", model.getName());
302306
}
303307
}
304308

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ModelTestTemplate.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.examplemodel.ExampleNode;
1313
import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaFile;
1414
import com.microsoft.typespec.http.client.generator.core.template.example.ModelExampleWriter;
15+
import com.microsoft.typespec.http.client.generator.core.util.ConstantStringTooLongException;
1516
import com.microsoft.typespec.http.client.generator.core.util.ModelExampleUtil;
1617
import com.microsoft.typespec.http.client.generator.core.util.ModelTestCaseUtil;
1718
import java.io.ByteArrayOutputStream;
@@ -32,6 +33,18 @@ public static ModelTestTemplate getInstance() {
3233
return INSTANCE;
3334
}
3435

36+
/**
37+
* Write the JSON serialization / de-serialization unit test for the model.
38+
*
39+
* @param model the client model to test.
40+
* @param javaFile the java file.
41+
* @throws com.microsoft.typespec.http.client.generator.core.util.PossibleCredentialException
42+
* thrown when there is possible mock value to a secret property.
43+
* Even when the value is mocked, it could be flagged by CI as issue. Therefore, the case is skipped.
44+
* @throws com.microsoft.typespec.http.client.generator.core.util.ConstantStringTooLongException
45+
* thrown when the String representation of the JSON is too long (>= 2^16).
46+
* Constant string of that size would cause compiler "constant string too long" error.
47+
*/
3548
@Override
3649
public void write(ClientModel model, JavaFile javaFile) {
3750

@@ -61,12 +74,20 @@ public void write(ClientModel model, JavaFile javaFile) {
6174

6275
javaFile.declareImport(imports);
6376

77+
String jsonStringExpression = ClassType.STRING.defaultValueExpression(jsonStr);
78+
if (jsonStringExpression.length() >= 65536) {
79+
// Java compiler would give "constant string too long" error on the generated file.
80+
// The length of a string constant in a class file is limited to 2^16 bytes in UTF-8 encoding.
81+
// There is also a related "code too large" error, for limit on Java method size in bytecode.
82+
throw new ConstantStringTooLongException();
83+
}
84+
6485
javaFile.publicFinalClass(model.getName() + "Tests", classBlock -> {
6586
// testDeserialize
6687
classBlock.annotation("org.junit.jupiter.api.Test");
6788
classBlock.publicMethod("void testDeserialize() throws Exception", methodBlock -> {
6889
methodBlock.line(String.format("%1$s model = BinaryData.fromString(%2$s).toObject(%1$s.class);",
69-
model.getName(), ClassType.STRING.defaultValueExpression(jsonStr)));
90+
model.getName(), jsonStringExpression));
7091
writer.writeAssertion(methodBlock);
7192
});
7293

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.typespec.http.client.generator.core.util;
5+
6+
public class ConstantStringTooLongException extends RuntimeException {
7+
8+
public ConstantStringTooLongException() {
9+
super("Constant string too long.");
10+
}
11+
}

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public static Object jsonFromType(int depth, IType type) {
9898
return RANDOM.nextInt() & Integer.MAX_VALUE;
9999
} else if (type.asNullable() == ClassType.LONG) {
100100
return RANDOM.nextLong() & Long.MAX_VALUE;
101+
} else if (type.asNullable() == ClassType.BIG_DECIMAL) {
102+
return RANDOM.nextLong() & Long.MAX_VALUE;
101103
} else if (type.asNullable() == ClassType.FLOAT) {
102104
return RANDOM.nextFloat() * 100;
103105
} else if (type.asNullable() == ClassType.DOUBLE) {

0 commit comments

Comments
 (0)