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 broken but supported protovalidate conformance tests #348

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.google.protobuf.DescriptorProtos.FileDescriptorProto
// https://github.com/protocolbuffers/protobuf/blob/5e84a6169cf0f9716c9285c95c860bcb355dbdc1/src/google/protobuf/stubs/strutil.cc#L595

// Limit the number of bytes per line.
private const val BYTES_PER_LINE = 40
private const val BYTES_PER_LINE = 200
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheapo version of #239 for now.


// Limit the number of lines per string part.
private const val LINES_PER_PART = 400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,16 @@ internal class FieldParser(
}

private fun optional(fdp: FieldDescriptorProto) =
when {
ctx.proto2 -> fdp.label == LABEL_OPTIONAL
ctx.proto3 -> fdp.proto3Optional
ctx.edition2023 -> optional(ctx.fileOptions.default.features, fdp.options.features)
else -> error("unexpected edition/syntax")
}
fdp.label != LABEL_REPEATED &&
when {
ctx.proto2 -> fdp.label == LABEL_OPTIONAL
ctx.proto3 -> fdp.proto3Optional
ctx.edition2023 -> optional(ctx.fileOptions.default.features, fdp.options.features)
else -> error("unexpected edition/syntax")
}

private fun optional(fileFeatures: FeatureSet, fieldFeatures: FeatureSet) =
if (fileFeatures.fieldPresence == FieldPresence.EXPLICIT) {
if (fileFeatures.fieldPresence == FieldPresence.EXPLICIT || !fileFeatures.hasFieldPresence()) {
fieldFeatures.fieldPresence !in setOf(FieldPresence.IMPLICIT, FieldPresence.LEGACY_REQUIRED)
} else {
fieldFeatures.fieldPresence == FieldPresence.EXPLICIT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2025 Toast, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

edition = "2023";

package protokt.v1.testing;

import "google/protobuf/empty.proto";

message TestFileDefault {
int32 foo = 1;
google.protobuf.Empty bar = 2;

int32 baz = 3 [features.field_presence = EXPLICIT];
google.protobuf.Empty qux = 4 [features.field_presence = EXPLICIT];

int32 corge = 5 [features.field_presence = IMPLICIT];
// not allowed: google.protobuf.Empty grault = 6 [features.field_presence = IMPLICIT];

int32 garply = 7 [features.field_presence = LEGACY_REQUIRED];
google.protobuf.Empty thud = 8 [features.field_presence = LEGACY_REQUIRED];
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,44 +61,87 @@ class Edition2023PresenceTest {

@Test
fun `file with explicit presence and no field features has correct behavior on primitive`() {
assertThat(TestFileImplicit::class.propertyIsMarkedNullable("foo")).isFalse()
assertThat(TestFileImplicit {}.foo).isEqualTo(0)
assertThat(TestFileExplicit::class.propertyIsMarkedNullable("foo")).isTrue()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was broken due to bad copypasta of the message type being asserted on.

assertThat(TestFileExplicit {}.foo).isNull()
}

@Test
fun `file with explicit presence and no field features has correct behavior on message`() {
assertThat(TestFileImplicit::class.propertyIsMarkedNullable("bar")).isTrue()
assertThat(TestFileImplicit {}.bar).isNull()
assertThat(TestFileExplicit::class.propertyIsMarkedNullable("bar")).isTrue()
assertThat(TestFileExplicit {}.bar).isNull()
}

@Test
fun `file with explicit presence and field with explicit presence has correct behavior on primitive`() {
assertThat(TestFileImplicit::class.propertyIsMarkedNullable("baz")).isTrue()
assertThat(TestFileImplicit {}.baz).isNull()
assertThat(TestFileExplicit::class.propertyIsMarkedNullable("baz")).isTrue()
assertThat(TestFileExplicit {}.baz).isNull()
}

@Test
fun `file with explicit presence and field with explicit presence has correct behavior on message`() {
assertThat(TestFileImplicit::class.propertyIsMarkedNullable("qux")).isTrue()
assertThat(TestFileImplicit {}.qux).isNull()
assertThat(TestFileExplicit::class.propertyIsMarkedNullable("qux")).isTrue()
assertThat(TestFileExplicit {}.qux).isNull()
}

@Test
fun `file with explicit presence and field with implicit presence has correct behavior on primitive`() {
assertThat(TestFileImplicit::class.propertyIsMarkedNullable("corge")).isFalse()
assertThat(TestFileImplicit {}.corge).isEqualTo(0)
assertThat(TestFileExplicit::class.propertyIsMarkedNullable("corge")).isFalse()
assertThat(TestFileExplicit {}.corge).isEqualTo(0)
}

@Test
fun `file with explicit presence and field with legacy required presence has correct behavior on primitive`() {
assertThat(TestFileImplicit::class.propertyIsMarkedNullable("garply")).isFalse()
assertThat(TestFileImplicit {}.garply).isEqualTo(0)
assertThat(TestFileExplicit::class.propertyIsMarkedNullable("garply")).isFalse()
assertThat(TestFileExplicit {}.garply).isEqualTo(0)
}

@Test
fun `file with explicit presence and field with legacy required for message type`() {
// protokt doesn't support non-null message types
assertThat(TestFileImplicit::class.propertyIsMarkedNullable("thud")).isTrue()
assertThat(TestFileImplicit {}.thud).isNull()
assertThat(TestFileExplicit::class.propertyIsMarkedNullable("thud")).isTrue()
assertThat(TestFileExplicit {}.thud).isNull()
}

@Test
fun `file without presence specified and no field features has correct behavior on primitive`() {
assertThat(TestFileDefault::class.propertyIsMarkedNullable("foo")).isTrue()
assertThat(TestFileDefault {}.foo).isNull()
}

@Test
fun `file without presence specified and no field features has correct behavior on message`() {
assertThat(TestFileDefault::class.propertyIsMarkedNullable("bar")).isTrue()
assertThat(TestFileDefault {}.bar).isNull()
}

@Test
fun `file without presence specified and field with explicit presence has correct behavior on primitive`() {
assertThat(TestFileDefault::class.propertyIsMarkedNullable("baz")).isTrue()
assertThat(TestFileDefault {}.baz).isNull()
}

@Test
fun `file without presence specified and field with explicit presence has correct behavior on message`() {
assertThat(TestFileDefault::class.propertyIsMarkedNullable("qux")).isTrue()
assertThat(TestFileDefault {}.qux).isNull()
}

@Test
fun `file without presence specified and field with implicit presence has correct behavior on primitive`() {
assertThat(TestFileDefault::class.propertyIsMarkedNullable("corge")).isFalse()
assertThat(TestFileDefault {}.corge).isEqualTo(0)
}

@Test
fun `file without presence specified and field with legacy required presence has correct behavior on primitive`() {
assertThat(TestFileDefault::class.propertyIsMarkedNullable("garply")).isFalse()
assertThat(TestFileDefault {}.garply).isEqualTo(0)
}

@Test
fun `file without presence specified and field with legacy required for message type`() {
// protokt doesn't support non-null message types
assertThat(TestFileDefault::class.propertyIsMarkedNullable("thud")).isTrue()
assertThat(TestFileDefault {}.thud).isNull()
}
}
Loading