Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static ModelTestTemplate getInstance() {
@Override
public void write(ClientModel model, JavaFile javaFile) {

// TODO: there is still possible "code too large" error
// example: https://github.com/Azure/azure-rest-api-specs/tree/main/specification/logic/resource-manager

final boolean immutableOutputModel = JavaSettings.getInstance().isOutputModelImmutable()
&& model.getImplementationDetails() != null
&& !model.getImplementationDetails().isInput();
Expand Down Expand Up @@ -66,7 +69,7 @@ public void write(ClientModel model, JavaFile javaFile) {
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(), getJavaStringExpression(jsonStr)));
writer.writeAssertion(methodBlock);
});

Expand All @@ -88,4 +91,25 @@ public void write(ClientModel model, JavaFile javaFile) {
}
});
}

private static String getJavaStringExpression(String jsonStr) {
// 2^16, but half the size, for there maybe lots of `\"` in JSON
final int maxConstantStringLength = 65536 / 2;
if (jsonStr.length() < maxConstantStringLength) {
return ClassType.STRING.defaultValueExpression(jsonStr);
} else {
// avoid "constant string too long"
StringBuilder sb = new StringBuilder("String.join(\"\"");
String remains = jsonStr;
String.join("", "", "");
while (remains.length() >= maxConstantStringLength) {
String current = remains.substring(0, maxConstantStringLength - 1);
remains = remains.substring(maxConstantStringLength - 1);

sb.append(", ").append(ClassType.STRING.defaultValueExpression(current));
}
sb.append(")");
return sb.toString();
}
}
}
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
Loading