|
| 1 | +// Copyright 2021-2025 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { test } from "node:test"; |
| 16 | +import { compileMessage } from "./helpers.js"; |
| 17 | +import { create, fromJson, toJson } from "@bufbuild/protobuf"; |
| 18 | +import { NullValue } from "@bufbuild/protobuf/wkt"; |
| 19 | +import assert from "node:assert"; |
| 20 | + |
| 21 | +void test("issue #1313", async () => { |
| 22 | + const descMessage = await compileMessage(` |
| 23 | + syntax="proto3"; |
| 24 | + import "google/protobuf/struct.proto"; |
| 25 | + message M { |
| 26 | + map<string, google.protobuf.Value> value_map = 1; |
| 27 | + map<string, google.protobuf.NullValue> null_value_map = 2; |
| 28 | + repeated google.protobuf.Value value_list = 3; |
| 29 | + repeated google.protobuf.NullValue null_value_list = 4; |
| 30 | + } |
| 31 | + `); |
| 32 | + const msg = create(descMessage, { |
| 33 | + valueMap: { |
| 34 | + val1: { |
| 35 | + kind: { case: "nullValue", value: NullValue.NULL_VALUE }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + nullValueMap: { |
| 39 | + val1: NullValue.NULL_VALUE, |
| 40 | + }, |
| 41 | + valueList: [ |
| 42 | + { |
| 43 | + kind: { case: "nullValue", value: NullValue.NULL_VALUE }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + nullValueList: [NullValue.NULL_VALUE], |
| 47 | + }); |
| 48 | + const json = toJson(descMessage, msg); |
| 49 | + assert.deepStrictEqual(json, { |
| 50 | + valueMap: { val1: null }, |
| 51 | + nullValueMap: { val1: null }, |
| 52 | + valueList: [null], |
| 53 | + nullValueList: [null], |
| 54 | + }); |
| 55 | + const msg2 = fromJson(descMessage, json); |
| 56 | + assert.deepStrictEqual(msg2, msg); |
| 57 | +}); |
0 commit comments