Skip to content

Commit

Permalink
http-client-java, generate mock Long number for BigDecimal (#5433)
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
weidongxu-microsoft authored Jan 3, 2025
1 parent 4fd8d1f commit 8f37063
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.microsoft.typespec.http.client.generator.core.template.Templates;
import com.microsoft.typespec.http.client.generator.core.template.TestProxyAssetsTemplate;
import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil;
import com.microsoft.typespec.http.client.generator.core.util.ConstantStringTooLongException;
import com.microsoft.typespec.http.client.generator.core.util.PossibleCredentialException;
import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -299,6 +300,9 @@ public void addModelUnitTest(ClientModel model) {
} catch (PossibleCredentialException e) {
// skip this test file
logger.warn("Skip unit test for model '{}', caused by key '{}'", model.getName(), e.getKeyName());
} catch (ConstantStringTooLongException e) {
// skip this test file
logger.warn("Skip unit test for model '{}', JSON string is too long.", model.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.examplemodel.ExampleNode;
import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaFile;
import com.microsoft.typespec.http.client.generator.core.template.example.ModelExampleWriter;
import com.microsoft.typespec.http.client.generator.core.util.ConstantStringTooLongException;
import com.microsoft.typespec.http.client.generator.core.util.ModelExampleUtil;
import com.microsoft.typespec.http.client.generator.core.util.ModelTestCaseUtil;
import java.io.ByteArrayOutputStream;
Expand All @@ -32,6 +33,18 @@ public static ModelTestTemplate getInstance() {
return INSTANCE;
}

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

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

javaFile.declareImport(imports);

String jsonStringExpression = ClassType.STRING.defaultValueExpression(jsonStr);
if (jsonStringExpression.length() >= 65536) {
// Java compiler would give "constant string too long" error on the generated file.
// The length of a string constant in a class file is limited to 2^16 bytes in UTF-8 encoding.
// There is also a related "code too large" error, for limit on Java method size in bytecode.
throw new ConstantStringTooLongException();
}

javaFile.publicFinalClass(model.getName() + "Tests", classBlock -> {
// testDeserialize
classBlock.annotation("org.junit.jupiter.api.Test");
classBlock.publicMethod("void testDeserialize() throws Exception", methodBlock -> {
methodBlock.line(String.format("%1$s model = BinaryData.fromString(%2$s).toObject(%1$s.class);",
model.getName(), ClassType.STRING.defaultValueExpression(jsonStr)));
model.getName(), jsonStringExpression));
writer.writeAssertion(methodBlock);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.typespec.http.client.generator.core.util;

public class ConstantStringTooLongException extends RuntimeException {

public ConstantStringTooLongException() {
super("Constant string too long.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public static Object jsonFromType(int depth, IType type) {
return RANDOM.nextInt() & Integer.MAX_VALUE;
} else if (type.asNullable() == ClassType.LONG) {
return RANDOM.nextLong() & Long.MAX_VALUE;
} else if (type.asNullable() == ClassType.BIG_DECIMAL) {
return RANDOM.nextLong() & Long.MAX_VALUE;
} else if (type.asNullable() == ClassType.FLOAT) {
return RANDOM.nextFloat() * 100;
} else if (type.asNullable() == ClassType.DOUBLE) {
Expand Down

0 comments on commit 8f37063

Please sign in to comment.