{"record":{"id":"328b54454653c5a8","repo":"apache/shenyu","slug":"can-not-get-defaultinstance-field-of-s","errorCode":null,"errorMessage":"can not get defaultInstance Field of %s","messagePattern":"can not get defaultInstance Field of (.+?)","errorType":"exception","errorClass":"ShenyuException","httpStatus":null,"severity":"error","filePath":"shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/json/JsonServerServiceInterceptor.java","lineNumber":89,"sourceCode":"     * @throws IllegalAccessException IllegalAccessException\n     */\n    public static <T> ServerServiceDefinition useMarshalledMessages(final ServerServiceDefinition serviceDef,\n                                                                    final MethodDescriptor.Marshaller<T> marshaller)\n            throws IllegalArgumentException, IllegalAccessException {\n        List<ServerMethodDefinition<?, ?>> wrappedMethods = new ArrayList<>();\n        List<MethodDescriptor<?, ?>> wrappedDescriptors = new ArrayList<>();\n\n        // Wrap the descriptors\n        for (final ServerMethodDefinition<?, ?> definition : serviceDef.getMethods()) {\n            MethodDescriptor.Marshaller<?> requestMarshaller = definition.getMethodDescriptor().getRequestMarshaller();\n            Field defaultInstanceField = ReflectUtils.getField(requestMarshaller.getClass(), \"defaultInstance\");\n            Class<?> grpcRequestParamClass = null;\n            if (Objects.isNull(defaultInstanceField)) {\n                // Compatible with lower versions. eg: grpc 1.6.0\n                if (requestMarshaller instanceof MethodDescriptor.PrototypeMarshaller) {\n                    MethodDescriptor.PrototypeMarshaller<?> prototypeMarshaller = (PrototypeMarshaller<?>) requestMarshaller;\n                    if (Objects.isNull(prototypeMarshaller.getMessagePrototype())) {\n                        throw new ShenyuException(String.format(\"can not get defaultInstance Field of %s\", requestMarshaller.getClass()));\n                    }\n                    grpcRequestParamClass = prototypeMarshaller.getMessagePrototype().getClass();\n                }\n            } else {\n                defaultInstanceField.setAccessible(true);\n                grpcRequestParamClass = defaultInstanceField.get(requestMarshaller).getClass();\n            }\n\n            String fullMethodName = definition.getMethodDescriptor().getFullMethodName();\n            MethodDescriptor.MethodType methodType = definition.getMethodDescriptor().getType();\n            METHOD_TYPE_MAP.put(fullMethodName, methodType);\n\n            String[] splitMethodName = fullMethodName.split(\"/\");\n            fullMethodName = splitMethodName[0] + GrpcConstants.GRPC_JSON_SERVICE + \"/\" + splitMethodName[1];\n            REQUEST_CLAZZ_MAP.put(fullMethodName, grpcRequestParamClass);\n\n            final MethodDescriptor<?, ?> originalMethodDescriptor = definition.getMethodDescriptor();\n            final MethodDescriptor<T, T> wrappedMethodDescriptor = originalMethodDescriptor","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/json/JsonServerServiceInterceptor.java#L71-L107","documentation":"JsonServerServiceInterceptor.useMarshalledMessages wraps gRPC method descriptors for JSON transcoding. It needs the request message's default instance: first via a 'defaultInstanceField' reflection field, falling back to MethodDescriptor.PrototypeMarshaller.getMessagePrototype(). If neither is available, it cannot determine the request type and throws ShenyuException naming the marshaller class.","triggerScenarios":"Calling useJsonMessages on a MethodDescriptor whose request marshaller is neither a ProtoLiteUtils marshaller exposing a defaultInstance field nor a PrototypeMarshaller with a non-null message prototype (grpc 1.6.0-era compatibility path).","commonSituations":"Custom marshallers not generated by grpc's protoc plugin; very old or very new grpc versions where field names/behavior changed; services using hand-written Marshallers.","solutions":["Use standard protobuf-generated marshallers (ProtoUtils.marshaller) for the service methods","Ensure the marshaller's getMessagePrototype() returns a non-null prototype when implementing PrototypeMarshaller","Upgrade/align grpc version so defaultInstance reflection works as expected","Catch ShenyuException and fall back to native (non-json) gRPC proxying"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"MethodDescriptor.PrototypeMarshaller<?> pm =\n    marshaller instanceof MethodDescriptor.PrototypeMarshaller\n        ? (MethodDescriptor.PrototypeMarshaller<?>) marshaller : null;\nif (pm == null && ReflectUtils.getField(marshaller.getClass(), \"defaultInstance\") == null) {\n    throw new IllegalStateException(\"marshaller exposes neither defaultInstance nor prototype\");\n}","typeGuard":null,"tryCatchPattern":"try { interceptor.useJsonMessages(methodDescriptor); }\ncatch (ShenyuException e) { log.error(\"json mode unsupported for this marshaller\", e); }","preventionTips":["Use ProtoUtils.marshaller-generated marshallers for json transcoding","Implement PrototypeMarshaller.getMessagePrototype() when writing custom marshallers","Test json mode after grpc version upgrades"],"tags":["grpc","reflection","marshaller","json-transcoding"],"backgroundTag":"incompatible-source-type","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}