Effect CommunityEC
Effect Community14mo ago
1 reply
jakubryska

Adding a Discriminator to a Union Schema in Effect Typescript

I wanted to add discriminator to the generated jsonSchema for my union schema type: https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/

I found out, that if the type of schema is Refinement, the jsonSchema will be merged, whereas if not, it will be replaced.
I think this is the code that is causing this behaviour:

https://github.com/Effect-TS/effect/blame/c8d20b0827a30e95a0fe192113e82b72c8bfb5cd/packages/platform/src/OpenApiJsonSchema.ts#L350

Since I did not need to do any refinements to the schema, I just added noop filter to the union to trigger the merge instead of replace.

union.pipe(Schema.filter((x) => true)).annotations({
  jsonSchema: {
    discriminator: {
      propertyName: '_tag',
      mapping,
    },
  },
});
Was this page helpful?