Skip to content

Commit a47fabf

Browse files
authored
Merge pull request #112 from mrt181/fix/nullable-enums
fix: nullable enums get VARCHAR type
2 parents 238e1ee + 8b98a2a commit a47fabf

File tree

7 files changed

+176
-419
lines changed

7 files changed

+176
-419
lines changed

Tests/ksqlDB.RestApi.Client.Tests/KSql/RestApi/Statements/CreateEntityTests.cs

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -759,29 +759,28 @@ public void ModelBuilder_IgnoreProperty()
759759
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
760760
}
761761

762-
private class PocoWithNullable : Poco
762+
private class Poco2 : Poco
763763
{
764764
public InnerPoco InnerPoco { get; init; }
765765
}
766766

767-
private class PocoWithNullable2 : Poco
767+
private class Poco3 : Poco
768768
{
769769
public InnerPoco InnerPoco { get; init; }
770770
}
771771

772-
private class PocoWithNullable3 : Poco
772+
private class Poco4 : Poco
773773
{
774774
public InnerPoco InnerPoco { get; init; }
775775
}
776776

777-
private class PocoWithNullable4 : Poco
777+
private class Poco5 : Poco
778778
{
779779
public InnerPoco InnerPoco { get; init; }
780780
}
781781

782782
private class OuterPoco : Poco
783783
{
784-
785784
public InnerPoco InnerPoco { get; init; }
786785
}
787786

@@ -796,17 +795,61 @@ private class InnerPoco2
796795
public int Value { get; init; }
797796
}
798797

798+
private class OuterPoco2 : Poco
799+
{
800+
public InnerPoco3 InnerPoco { get; init; }
801+
}
802+
803+
private class InnerPoco3
804+
{
805+
public int Value { get; init; }
806+
public PocoEnum? ValueNullable { get; init; }
807+
}
808+
809+
private enum PocoEnum
810+
{
811+
MemberA,
812+
}
813+
814+
[Test]
815+
public void ModelBuilder_SetTypeOfNullableEnum()
816+
{
817+
//Arrange
818+
modelBuilder.Entity<OuterPoco2>()
819+
.HasKey(x => x.Id);
820+
modelBuilder.Entity<OuterPoco2>()
821+
.Property(c => c.InnerPoco).AsStruct();
822+
823+
var statementContext = new StatementContext
824+
{
825+
CreationType = CreationType.CreateOrReplace,
826+
KSqlEntityType = KSqlEntityType.Table,
827+
};
828+
829+
creationMetadata.KafkaTopic = nameof(Poco);
830+
831+
//Act
832+
string statement = new CreateEntity(modelBuilder).Print<OuterPoco2>(statementContext, creationMetadata, null);
833+
834+
//Assert
835+
statement.Should().Be($@"CREATE OR REPLACE TABLE {nameof(OuterPoco2)}s (
836+
{nameof(OuterPoco2.InnerPoco)} STRUCT<Value INT, ValueNullable VARCHAR>,
837+
{nameof(OuterPoco2.Id)} INT PRIMARY KEY,
838+
{nameof(OuterPoco2.Description)} VARCHAR
839+
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
840+
}
841+
799842
[Test]
800843
public void ModelBuilder_IgnoreStructProperty()
801844
{
802845
//Arrange
803-
modelBuilder.Entity<PocoWithNullable>()
846+
modelBuilder.Entity<Poco2>()
804847
.HasKey(x => x.Id);
805-
modelBuilder.Entity<PocoWithNullable>()
848+
modelBuilder.Entity<Poco2>()
806849
.Property(c => c.InnerPoco).AsStruct();
807-
modelBuilder.Entity<PocoWithNullable>()
850+
modelBuilder.Entity<Poco2>()
808851
.Property(c => c.InnerPoco.InnerPoco2).Ignore();
809-
modelBuilder.Entity<PocoWithNullable>()
852+
modelBuilder.Entity<Poco2>()
810853
.Property(c => c.InnerPoco.Value);
811854

812855
var statementContext = new StatementContext
@@ -818,11 +861,11 @@ public void ModelBuilder_IgnoreStructProperty()
818861
creationMetadata.KafkaTopic = nameof(Poco);
819862

820863
//Act
821-
string statement = new CreateEntity(modelBuilder).Print<PocoWithNullable>(statementContext, creationMetadata, null);
864+
string statement = new CreateEntity(modelBuilder).Print<Poco2>(statementContext, creationMetadata, null);
822865

823866
//Assert
824-
statement.Should().Be($@"CREATE OR REPLACE TABLE {nameof(PocoWithNullable)}s (
825-
{nameof(PocoWithNullable.InnerPoco)} STRUCT<Value VARCHAR>,
867+
statement.Should().Be($@"CREATE OR REPLACE TABLE {nameof(Poco2)}s (
868+
{nameof(Poco2.InnerPoco)} STRUCT<Value VARCHAR>,
826869
{nameof(Poco.Id)} INT PRIMARY KEY,
827870
{nameof(Poco.Description)} VARCHAR
828871
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
@@ -832,36 +875,36 @@ public void ModelBuilder_IgnoreStructProperty()
832875
public void ModelBuilder_IgnoreStructPropertyOnOneEntityButNotOnAnother()
833876
{
834877
//Arrange
835-
modelBuilder.Entity<PocoWithNullable>()
878+
modelBuilder.Entity<Poco2>()
836879
.HasKey(x => x.Id);
837-
modelBuilder.Entity<PocoWithNullable>()
880+
modelBuilder.Entity<Poco2>()
838881
.Property(c => c.InnerPoco).AsStruct();
839-
modelBuilder.Entity<PocoWithNullable>()
882+
modelBuilder.Entity<Poco2>()
840883
.Property(c => c.InnerPoco.InnerPoco2).Ignore();
841884

842-
modelBuilder.Entity<PocoWithNullable2>()
885+
modelBuilder.Entity<Poco3>()
843886
.HasKey(x => x.Id);
844-
modelBuilder.Entity<PocoWithNullable2>()
887+
modelBuilder.Entity<Poco3>()
845888
.Property(c => c.InnerPoco).AsStruct();
846-
modelBuilder.Entity<PocoWithNullable2>()
889+
modelBuilder.Entity<Poco3>()
847890
.Property(c => c.InnerPoco.Value).Ignore();
848-
modelBuilder.Entity<PocoWithNullable2>()
891+
modelBuilder.Entity<Poco3>()
849892
.Property(c => c.InnerPoco.InnerPoco2).AsStruct();
850893

851-
modelBuilder.Entity<PocoWithNullable3>()
894+
modelBuilder.Entity<Poco4>()
852895
.HasKey(x => x.Id);
853-
modelBuilder.Entity<PocoWithNullable3>()
896+
modelBuilder.Entity<Poco4>()
854897
.Property(c => c.InnerPoco).AsStruct();
855-
modelBuilder.Entity<PocoWithNullable3>()
898+
modelBuilder.Entity<Poco4>()
856899
.Property(c => c.InnerPoco.InnerPoco2).AsStruct();
857-
modelBuilder.Entity<PocoWithNullable3>()
900+
modelBuilder.Entity<Poco4>()
858901
.Property(c => c.InnerPoco.InnerPoco2!.FirstOrDefault()!.Value).Ignore();
859902

860-
modelBuilder.Entity<PocoWithNullable4>()
903+
modelBuilder.Entity<Poco5>()
861904
.HasKey(x => x.Id);
862-
modelBuilder.Entity<PocoWithNullable4>()
905+
modelBuilder.Entity<Poco5>()
863906
.Property(c => c.InnerPoco).AsStruct();
864-
modelBuilder.Entity<PocoWithNullable4>()
907+
modelBuilder.Entity<Poco5>()
865908
.Property(c => c.InnerPoco.InnerPoco2).AsStruct();
866909

867910
var statementContext = new StatementContext
@@ -873,32 +916,32 @@ public void ModelBuilder_IgnoreStructPropertyOnOneEntityButNotOnAnother()
873916
creationMetadata.KafkaTopic = nameof(Poco);
874917

875918
//Act
876-
var statement1 = new CreateEntity(modelBuilder).Print<PocoWithNullable>(statementContext, creationMetadata, null);
877-
var statement2 = new CreateEntity(modelBuilder).Print<PocoWithNullable2>(statementContext, creationMetadata, null);
878-
var statement3 = new CreateEntity(modelBuilder).Print<PocoWithNullable3>(statementContext, creationMetadata, null);
879-
var statement4 = new CreateEntity(modelBuilder).Print<PocoWithNullable4>(statementContext, creationMetadata, null);
919+
var statement1 = new CreateEntity(modelBuilder).Print<Poco2>(statementContext, creationMetadata, null);
920+
var statement2 = new CreateEntity(modelBuilder).Print<Poco3>(statementContext, creationMetadata, null);
921+
var statement3 = new CreateEntity(modelBuilder).Print<Poco4>(statementContext, creationMetadata, null);
922+
var statement4 = new CreateEntity(modelBuilder).Print<Poco5>(statementContext, creationMetadata, null);
880923

881924
//Assert
882-
statement1.Should().Be($@"CREATE OR REPLACE TABLE {nameof(PocoWithNullable)}s (
883-
{nameof(PocoWithNullable.InnerPoco)} STRUCT<Value VARCHAR>,
925+
statement1.Should().Be($@"CREATE OR REPLACE TABLE {nameof(Poco2)}s (
926+
{nameof(Poco2.InnerPoco)} STRUCT<Value VARCHAR>,
884927
{nameof(Poco.Id)} INT PRIMARY KEY,
885928
{nameof(Poco.Description)} VARCHAR
886929
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
887930

888-
statement2.Should().Be($@"CREATE OR REPLACE TABLE {nameof(PocoWithNullable2)}s (
889-
{nameof(PocoWithNullable2.InnerPoco)} STRUCT<{nameof(PocoWithNullable2.InnerPoco.InnerPoco2)} ARRAY<STRUCT<{nameof(InnerPoco2.Value)} INT>>>,
931+
statement2.Should().Be($@"CREATE OR REPLACE TABLE {nameof(Poco3)}s (
932+
{nameof(Poco3.InnerPoco)} STRUCT<{nameof(Poco3.InnerPoco.InnerPoco2)} ARRAY<STRUCT<{nameof(InnerPoco2.Value)} INT>>>,
890933
{nameof(Poco.Id)} INT PRIMARY KEY,
891934
{nameof(Poco.Description)} VARCHAR
892935
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
893936

894-
statement3.Should().Be($@"CREATE OR REPLACE TABLE {nameof(PocoWithNullable3)}s (
895-
{nameof(PocoWithNullable3.InnerPoco)} STRUCT<Value VARCHAR>,
937+
statement3.Should().Be($@"CREATE OR REPLACE TABLE {nameof(Poco4)}s (
938+
{nameof(Poco4.InnerPoco)} STRUCT<Value VARCHAR>,
896939
{nameof(Poco.Id)} INT PRIMARY KEY,
897940
{nameof(Poco.Description)} VARCHAR
898941
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
899942

900-
statement4.Should().Be($@"CREATE OR REPLACE TABLE {nameof(PocoWithNullable4)}s (
901-
{nameof(PocoWithNullable4.InnerPoco)} STRUCT<{nameof(PocoWithNullable4.InnerPoco.Value)} VARCHAR, {nameof(PocoWithNullable4.InnerPoco.InnerPoco2)} ARRAY<STRUCT<{nameof(InnerPoco2.Value)} INT>>>,
943+
statement4.Should().Be($@"CREATE OR REPLACE TABLE {nameof(Poco5)}s (
944+
{nameof(Poco5.InnerPoco)} STRUCT<{nameof(Poco5.InnerPoco.Value)} VARCHAR, {nameof(Poco5.InnerPoco.InnerPoco2)} ARRAY<STRUCT<{nameof(InnerPoco2.Value)} INT>>>,
902945
{nameof(Poco.Id)} INT PRIMARY KEY,
903946
{nameof(Poco.Description)} VARCHAR
904947
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
@@ -960,12 +1003,12 @@ public void ModelBuilder_IgnoreStructWithAllIgnoredFieldsProperty()
9601003
// STRUCT<> is an invalid ksql statement
9611004

9621005
//Arrange
963-
modelBuilder.Entity<PocoWithNullable>()
1006+
modelBuilder.Entity<Poco2>()
9641007
.HasKey(x => x.Id)
9651008
.Property(c => c.InnerPoco).AsStruct();
966-
modelBuilder.Entity<PocoWithNullable>()
1009+
modelBuilder.Entity<Poco2>()
9671010
.Property(c => c.InnerPoco.InnerPoco2).Ignore();
968-
modelBuilder.Entity<PocoWithNullable>()
1011+
modelBuilder.Entity<Poco2>()
9691012
.Property(c => c.InnerPoco.Value).Ignore();
9701013

9711014
var statementContext = new StatementContext
@@ -977,10 +1020,10 @@ public void ModelBuilder_IgnoreStructWithAllIgnoredFieldsProperty()
9771020
creationMetadata.KafkaTopic = nameof(Poco);
9781021

9791022
//Act
980-
string statement = new CreateEntity(modelBuilder).Print<PocoWithNullable>(statementContext, creationMetadata, null);
1023+
string statement = new CreateEntity(modelBuilder).Print<Poco2>(statementContext, creationMetadata, null);
9811024

9821025
//Assert
983-
statement.Should().Be($@"CREATE OR REPLACE TABLE {nameof(PocoWithNullable)}s (
1026+
statement.Should().Be($@"CREATE OR REPLACE TABLE {nameof(Poco2)}s (
9841027
{nameof(Poco.Id)} INT PRIMARY KEY,
9851028
{nameof(Poco.Description)} VARCHAR
9861029
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());
@@ -1000,12 +1043,12 @@ public void ModelBuilder_SubPropertiesOfAnIgnoredMemberAreSkipped()
10001043
// STRUCT<> is an invalid ksql statement
10011044

10021045
//Arrange
1003-
modelBuilder.Entity<PocoWithNullable>()
1046+
modelBuilder.Entity<Poco2>()
10041047
.HasKey(x => x.Id)
10051048
.Property(c => c.InnerPoco).Ignore();
1006-
modelBuilder.Entity<PocoWithNullable>()
1049+
modelBuilder.Entity<Poco2>()
10071050
.Property(c => c.InnerPoco.InnerPoco2);
1008-
modelBuilder.Entity<PocoWithNullable>()
1051+
modelBuilder.Entity<Poco2>()
10091052
.Property(c => c.InnerPoco.Value);
10101053

10111054
var statementContext = new StatementContext
@@ -1017,10 +1060,10 @@ public void ModelBuilder_SubPropertiesOfAnIgnoredMemberAreSkipped()
10171060
creationMetadata.KafkaTopic = nameof(Poco);
10181061

10191062
//Act
1020-
string statement = new CreateEntity(modelBuilder).Print<PocoWithNullable>(statementContext, creationMetadata, null);
1063+
string statement = new CreateEntity(modelBuilder).Print<Poco2>(statementContext, creationMetadata, null);
10211064

10221065
//Assert
1023-
statement.Should().Be($@"CREATE OR REPLACE TABLE {nameof(PocoWithNullable)}s (
1066+
statement.Should().Be($@"CREATE OR REPLACE TABLE {nameof(Poco2)}s (
10241067
{nameof(Poco.Id)} INT PRIMARY KEY,
10251068
{nameof(Poco.Description)} VARCHAR
10261069
) WITH ( KAFKA_TOPIC='{nameof(Poco)}', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' );".ReplaceLineEndings());

0 commit comments

Comments
 (0)