Skip to content

Commit

Permalink
Fix caching in the openapi module for classes abstracted over types
Browse files Browse the repository at this point in the history
  • Loading branch information
pwliwanow committed Oct 19, 2024
1 parent b38aea0 commit c1662bd
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 119 deletions.
69 changes: 34 additions & 35 deletions chopsticks-csv/src/main/scala/dev/chopsticks/csv/CsvDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.chopsticks.csv
import dev.chopsticks.openapi.{OpenApiParsedAnnotations, OpenApiSumTypeSerDeStrategy, OpenApiValidation}
import dev.chopsticks.openapi.common.{ConverterCache, OpenApiConverterUtils}
import sttp.tapir.Validator
import zio.schema.{FieldSet, Schema, StandardType, TypeId}
import zio.schema.{Schema, StandardType}
import zio.Chunk
import zio.schema.Schema.Primitive

Expand Down Expand Up @@ -448,10 +448,9 @@ object CsvDecoder extends CsvProductDecoders {
cache: ConverterCache[CsvDecoder] = new ConverterCache[CsvDecoder]()
) {
private def convertUsingCache[A](
typeId: TypeId,
annotations: OpenApiParsedAnnotations[A]
schema: Schema[A]
)(convert: => CsvDecoder[A]): CsvDecoder[A] = {
cache.convertUsingCache(typeId, annotations)(convert)(() => new LazyDecoder[A]())
cache.convertUsingCache(schema)(convert)(() => new LazyDecoder[A]())
}

// scalafmt: { maxColumn = 800, optIn.configStyleArguments = false }
Expand Down Expand Up @@ -483,19 +482,19 @@ object CsvDecoder extends CsvProductDecoders {
case l @ Schema.Lazy(_) =>
convert(l.schema)

case Schema.GenericRecord(id, fieldSet, annotations) =>
genericRecordConverter(id, fieldSet, annotations)
case s @ Schema.GenericRecord(_, _, _) =>
genericRecordConverter(s)

case Schema.CaseClass0(id, construct, annotations) =>
case Schema.CaseClass0(_, construct, annotations) =>
val parsed = extractAnnotations[A](annotations)
convertUsingCache(id, parsed) {
convertUsingCache(schema) {
val baseDecoder = caseClass0Decoder(construct)
addAnnotations(baseDecoder, parsed)
}

case Schema.CaseClass1(id, f1, construct, annotations) =>
case Schema.CaseClass1(_, f1, construct, annotations) =>
val parsed = extractAnnotations[A](annotations)
convertUsingCache(id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(f1.schema), extractAnnotations(f1.annotations))
val baseDecoder = forProduct1(
options = options,
Expand All @@ -507,7 +506,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass2(_, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val baseDecoder = forProduct2(
Expand All @@ -523,7 +522,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass3(_, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand All @@ -541,7 +540,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass4(_, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand All @@ -561,7 +560,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass5(_, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand All @@ -583,7 +582,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass6(_, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand All @@ -607,7 +606,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass7(_, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand All @@ -633,7 +632,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass8(_, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -661,7 +660,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass9(_, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -691,7 +690,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass10(_, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -723,7 +722,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass11(_, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -757,7 +756,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass12(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -793,7 +792,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass13(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -831,7 +830,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass14(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -871,7 +870,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass15(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -913,7 +912,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass16(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -957,7 +956,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass17(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -1003,7 +1002,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass18(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -1051,7 +1050,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass19(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -1101,7 +1100,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass20(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -1153,7 +1152,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass21(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -1207,7 +1206,7 @@ object CsvDecoder extends CsvProductDecoders {

case s @ Schema.CaseClass22(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) =>
val parsed = extractAnnotations[A](s.annotations)
convertUsingCache(s.id, parsed) {
convertUsingCache(schema) {
val decoder1 = addAnnotations(convert(s.field1.schema), extractAnnotations(s.field1.annotations))
val decoder2 = addAnnotations(convert(s.field2.schema), extractAnnotations(s.field2.annotations))
val decoder3 = addAnnotations(convert(s.field3.schema), extractAnnotations(s.field3.annotations))
Expand Down Expand Up @@ -1356,12 +1355,12 @@ object CsvDecoder extends CsvProductDecoders {

@SuppressWarnings(Array("org.wartremover.warts.JavaSerializable"))
private def genericRecordConverter(
id: TypeId,
fieldSet: FieldSet,
annotations: Chunk[Any]
recordSchema: Schema.GenericRecord
): CsvDecoder[ListMap[String, _]] = {
val fieldSet = recordSchema.fieldSet
val annotations = recordSchema.annotations
val parsed = extractAnnotations[ListMap[String, _]](annotations)
convertUsingCache(id, parsed) {
convertUsingCache(recordSchema) {
val fieldDecoders = fieldSet.toChunk.iterator
.map { field =>
val fieldDecoder = addAnnotations(convert(field.schema), extractAnnotations(field.annotations))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dev.chopsticks.openapi.common.{ConverterCache, OpenApiConverterUtils}
import org.apache.commons.text.StringEscapeUtils
import zio.schema.{Schema, StandardType, TypeId}
import zio.{Chunk, ChunkBuilder}
import zio.schema.Schema.{Field, Primitive}
import zio.schema.Schema.Primitive

import java.time.{
DayOfWeek,
Expand Down Expand Up @@ -346,10 +346,9 @@ object CsvEncoder {
cache: ConverterCache[CsvEncoder] = new ConverterCache[CsvEncoder]()
) {
private def convertUsingCache[A](
typeId: TypeId,
annotations: OpenApiParsedAnnotations[A]
schema: Schema[A]
)(convert: => CsvEncoder[A]): CsvEncoder[A] = {
cache.convertUsingCache(typeId, annotations)(convert)(() => new LazyEncoder[A]())
cache.convertUsingCache(schema)(convert)(() => new LazyEncoder[A]())
}

// scalafmt: { maxColumn = 800, optIn.configStyleArguments = false }
Expand Down Expand Up @@ -389,7 +388,7 @@ object CsvEncoder {
convert(l.schema)

case s: Schema.Record[A] =>
convertRecord[A](s.id, s.annotations, s.fields)
convertRecord[A](s)

case s: Schema.Enum[A] =>
convertEnum[A](s.id, s.annotations, s.cases)
Expand All @@ -401,12 +400,13 @@ object CsvEncoder {
// scalafmt: { maxColumn = 120, optIn.configStyleArguments = true }

private def convertRecord[A](
id: TypeId,
annotations: Chunk[Any],
fields: Chunk[Field[A, _]]
record: Schema.Record[A]
): CsvEncoder[A] = {
val id = record.id
val annotations = record.annotations
val fields = record.fields
val recordAnnotations: OpenApiParsedAnnotations[A] = extractAnnotations[A](annotations)
convertUsingCache(id, recordAnnotations) {
convertUsingCache(record) {
val fieldEncoders = fields
.map { field =>
addAnnotations(None, convert(field.schema), extractAnnotations(field.annotations))
Expand Down
Loading

0 comments on commit c1662bd

Please sign in to comment.