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

Fix jruby encoding in new thread (#12919) #12930

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 10 additions & 4 deletions ruby/src/main/java/com/google/protobuf/jruby/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto;
import com.google.protobuf.Descriptors.FieldDescriptor;
import java.math.BigInteger;
import org.jcodings.Encoding;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.UTF8Encoding;
import org.jruby.*;
import org.jruby.exceptions.RaiseException;
import org.jruby.ext.bigdecimal.RubyBigDecimal;
Expand Down Expand Up @@ -141,12 +143,12 @@ public static IRubyObject checkType(
throw createInvalidTypeError(context, "boolean", fieldName, value);
break;
case BYTES:
value = validateAndEncodeString(context, "bytes", fieldName, value, "Encoding::ASCII_8BIT");
value = validateAndEncodeString(context, "bytes", fieldName, value, ASCIIEncoding.INSTANCE);
break;
case STRING:
value =
validateAndEncodeString(
context, "string", fieldName, symToString(value), "Encoding::UTF_8");
context, "string", fieldName, symToString(value), UTF8Encoding.INSTANCE);
break;
case MESSAGE:
if (value.getMetaClass() != typeClass) {
Expand Down Expand Up @@ -383,11 +385,15 @@ private static IRubyObject validateAndEncodeString(
String fieldType,
String fieldName,
IRubyObject value,
String encoding) {
Encoding encoding) {
if (!(value instanceof RubyString))
throw createInvalidTypeError(context, fieldType, fieldName, value);

value = ((RubyString) value).encode(context, context.runtime.evalScriptlet(encoding));
value =
((RubyString) value)
.encode(
context,
context.runtime.getEncodingService().convertEncodingToRubyEncoding(encoding));
value.setFrozen(true);
return value;
}
Expand Down