You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature: True C# Polymorphism and naturally strongly typed DTOs #921
Is it intended in the future to add features such as true inheritance to the generator, for c#-native polymorphism, instead of the unsafe json property access nowadays.
Another severe convenience would be the possibility of a simpler strongly typed object hirachy, alongside the feature heavy api that disencourages uninstructed users. (I plan to hand my generated json-shema objects to clients who do not necessarily know about the json-shema and neither want to acquire the skill to operate the corvus api.)
I figured that NJsonSchema does a decent job on these two, but it is a pre-build-generator.
I am really interested "if" and "when" these features are planned, since they are sadly deal-breakers for me. Still Corvus.JsonShema does an excellent job elsewhere, which really encourages me to use it.
We do not intend to implement C# polymorphism as it is not a good fit for JSON composition - this is why most code generators (like NJsonSchema) fail for anything but the simplest schema.
If you have examples of some schema you use, and the bits of the generated code you would like to be able to omit, please post them to this thread, and it would be interesting to see what I can do.
(Also, are you looking at the V4 generator, or the V5 generator?)
I am looking into the scope of a code generation engine that will leverage CTJ serialization for standard POCO objects backed by an internal "validation only" version of our zero-alloc types. While this wouldn't be anything like as efficient, it would give you the full-fidelity, high performance validation with a simpler API for your clients.
We do not intend to implement C# polymorphism as it is not a good fit for JSON composition - this is why most code generators (like NJsonSchema) fail for anything but the simplest schema.
If you have examples of some schema you use, and the bits of the generated code you would like to be able to omit, please post them to this thread, and it would be interesting to see what I can do.
(Also, are you looking at the V4 generator, or the V5 generator?)
I am looking for the V5 generator.
Buit I now think that corvus was designed on purpose to not meet my idea of a "simple" api.
Basically my dream would be to have a json-shema powered config framework that generates an apropriate ui. Therefore I need: config defined by json-shema => Data coming from json or the ui => validation => some patches due to config-templates to keep ui clean => another validation => parsing to generated c# objects for strongly typed, object-oriented and read-only access.
About the last point I am a bit unsure because of the strong requirements to the shema to make object orientation work.
I like the modern, flexible approach of corvus that enables more freedom when writing shemas and uses performant incremental generators. Taking advantage of polymorphism tough might still be beneficial to us as our config framework will be highly object oriented, while being not to crazy in shema complexity...
Regarding api simplicity: I did not use the api in detail to provide good critics, but I find it hard to cast the strongly typed properties. It is hard to infere the type from the generic entities via Intellisense. The only way I see is to look up the shema for the property. Other than that there is no way to know if you are working with a string a bool or a number, right? Also is there a good reason against generating enums?
Example shema: (the described type will be used inside a oneOf[] in pure form to make oop work.)
$schema: https://json-schema.org/draft/2020-12/schema?custom
$id: "https://pv-barber.local/schemas/algorithms/base/algorithm_config_base"
title: "Base algorithm config"
type: object
required:
- type
properties:
enabled:
type: boolean
description: "Whether this algorithm is enabled"
default: true
type:
type: string
description: "The type of this algorithm"
examples:
- "PhotovoltaicCharging"
- "SmartPeakShaving"
- "ZeroGridImport"
$id: "https://pv-barber.local/schemas/algorithms/photovoltaic_charging_config"
title: "Photovoltaic Charging Algorithm"
description: "An algorithm that controls the storage charging behaviour to achieve zero grid export power"
type: object
unevaluatedProperties: false
allOf:
- $ref: "https://pv-barber.local/schemas/algorithms/base/algorithm_config_base"
- type: object
required:
- type
properties:
type:
const: PhotovoltaicCharging
options:
type: object
description: "Options for this photovoltaic charging algorithm"
properties:
grid_export_power_activate:
type: integer
description: "Set the minimum grid export power needed before this algorithm takes affect (Unit: W)"
default: 0
minimum: 0
grid_import_power_deactivate:
type: integer
description: "Set the minimum grid import power needed before this algorithm is deactivated (Unit: W)"
default: 100
minimum: 0
grid_import_power_target:
type: integer
description: "Set the grid import power that should be achieved (Unit: W)"
default: 0
I am looking into the scope of a code generation engine that will leverage CTJ serialization for standard POCO objects backed by an internal "validation only" version of our zero-alloc types. While this wouldn't be anything like as efficient, it would give you the full-fidelity, high performance validation with a simpler API for your clients.
I am curious, is it pure serialization? If so it would be no use in my case...
@stadlaa - yes, that's no use to you (or me)! I have parked that suggestion.
I am working on exactly that UI generation side; I leverage the work we do to infer semantic information from the schema, and use that to generate (in my case HTML) UI as part of the Arazzo workflow work. It has a fair few holes right now, but I am aiming to have a first preview up in the next few weeks. It may offer some suggestions.
@stadlaa This is an interesting point: "as our config framework will be highly object oriented"
Object-oriented doesn't just mean "inheritance-based"; composition is an OO pattern too - and I would argue usually better than an inheritance one; particularly on a platform that favours a single base-class.
I am interested to understand why you are designing a heavily inheritance-based system when your constraint language (JSON Schema) is inherently compositional? (A genuine question, as we see this antipattern quite often.)
Secondly, your point about Intellisense is a good one; we should provide something to help with that.
I am adopting right now,
reconsidering some design choices after I fell in love with free will in schema design...
Her is why:
My initial concern stems from preexisting reflection-based factories that take the constant discriminator and refactors it directly to a type and forward the config as an abstract base type into the dynamically built constructor. This moves handling of specific variants into the consuming class and keeps the factory code slim.
I discarded it and desided for a rewrite utilizing .Match() which does not feel smelly at all anymore, leaving me with the hussle to implement the Match delegates which I decided to deal with.
(I am still a beginner as a developer and architect.)
Concretising persisting suggestions
Still the thought of turning off potentially unskilled plugin-devs that are most-likely home-lab-coders which are not familliar with corvus and are asked to implement a json-schema to get a ui-generated-config for there class is a concern.
Accessing schema combinations with .Match() and enumerating with .EnumerateArray() or all of the necessary casts for property access + missing IntelliSense have to be well documented and can present serious hurdles for .net beginners. (Any second opinion on this concern is welcome, maybe I am overthinking)
I wonder if there is a possibility to linder those issues altough I see that getting rid of the pattern-matching would seriously affect json-schema compatability and is probably not really a viable option.
Vague suggestion: Being able to hide json-shema related members like .Evaluate() .CreateBuilder() .Freeze() .IsValid() .Validate() could have some impact on consumer experiense, leaving them with the pure consumer features.
If possible: Getting rid of .EnumerateArray() in favour of some variant of IEnumerable and enabling implicit cast for all properties would have even more benefit.
@mwadams: It seems you put a lot of time, expertise and care into this project. Really enjoy it!! And I love to see source gens take over the old reflection monsters!
Now, I can provide you with IEnumerable very easily... I did in v4 but I wanted to see whether it was necessary in V5 before expanding the surface area again. However, you only get that on types that are pure arrays.
Thanks for the discussion here. C# inheritance is not something we will add, because JSON Schema composes rather than inherits, but two of the suggestions are now issues of their own:
Activity
mwadams commentedon Aug 5, 2026
We do not intend to implement C# polymorphism as it is not a good fit for JSON composition - this is why most code generators (like NJsonSchema) fail for anything but the simplest schema.
If you have examples of some schema you use, and the bits of the generated code you would like to be able to omit, please post them to this thread, and it would be interesting to see what I can do.
(Also, are you looking at the V4 generator, or the V5 generator?)
mwadams commentedon Aug 6, 2026
I am looking into the scope of a code generation engine that will leverage CTJ serialization for standard POCO objects backed by an
internal"validation only" version of our zero-alloc types. While this wouldn't be anything like as efficient, it would give you the full-fidelity, high performance validation with a simpler API for your clients.stadlaa commentedon Aug 6, 2026
I am looking for the V5 generator.
Buit I now think that corvus was designed on purpose to not meet my idea of a "simple" api.
Basically my dream would be to have a json-shema powered config framework that generates an apropriate ui. Therefore I need: config defined by json-shema => Data coming from json or the ui => validation => some patches due to config-templates to keep ui clean => another validation => parsing to generated c# objects for strongly typed, object-oriented and read-only access.
About the last point I am a bit unsure because of the strong requirements to the shema to make object orientation work.
I like the modern, flexible approach of corvus that enables more freedom when writing shemas and uses performant incremental generators. Taking advantage of polymorphism tough might still be beneficial to us as our config framework will be highly object oriented, while being not to crazy in shema complexity...
Regarding api simplicity: I did not use the api in detail to provide good critics, but I find it hard to cast the strongly typed properties. It is hard to infere the type from the generic entities via Intellisense. The only way I see is to look up the shema for the property. Other than that there is no way to know if you are working with a string a bool or a number, right? Also is there a good reason against generating enums?
Example shema: (the described type will be used inside a oneOf[] in pure form to make oop work.)
stadlaa commentedon Aug 6, 2026
I am curious, is it pure serialization? If so it would be no use in my case...
mwadams commentedon Aug 7, 2026
@stadlaa - yes, that's no use to you (or me)! I have parked that suggestion.
I am working on exactly that UI generation side; I leverage the work we do to infer semantic information from the schema, and use that to generate (in my case HTML) UI as part of the Arazzo workflow work. It has a fair few holes right now, but I am aiming to have a first preview up in the next few weeks. It may offer some suggestions.
mwadams commentedon Aug 10, 2026
@stadlaa This is an interesting point: "as our config framework will be highly object oriented"
Object-oriented doesn't just mean "inheritance-based"; composition is an OO pattern too - and I would argue usually better than an inheritance one; particularly on a platform that favours a single base-class.
I am interested to understand why you are designing a heavily inheritance-based system when your constraint language (JSON Schema) is inherently compositional? (A genuine question, as we see this antipattern quite often.)
Secondly, your point about Intellisense is a good one; we should provide something to help with that.
mwadams commentedon Aug 10, 2026
Thanks for raising this, BTW - even if you don't end up adopting, these are exactly the issues we want to engage with.
stadlaa commentedon Aug 11, 2026
I am adopting right now,
reconsidering some design choices after I fell in love with free will in schema design...
Her is why:
My initial concern stems from preexisting reflection-based factories that take the constant discriminator and refactors it directly to a type and forward the config as an abstract base type into the dynamically built constructor. This moves handling of specific variants into the consuming class and keeps the factory code slim.
I discarded it and desided for a rewrite utilizing .Match() which does not feel smelly at all anymore, leaving me with the hussle to implement the Match delegates which I decided to deal with.
(I am still a beginner as a developer and architect.)
Concretising persisting suggestions
Still the thought of turning off potentially unskilled plugin-devs that are most-likely home-lab-coders which are not familliar with corvus and are asked to implement a json-schema to get a ui-generated-config for there class is a concern.
Accessing schema combinations with .Match() and enumerating with .EnumerateArray() or all of the necessary casts for property access + missing IntelliSense have to be well documented and can present serious hurdles for .net beginners. (Any second opinion on this concern is welcome, maybe I am overthinking)
I wonder if there is a possibility to linder those issues altough I see that getting rid of the pattern-matching would seriously affect json-schema compatability and is probably not really a viable option.
Vague suggestion: Being able to hide json-shema related members like .Evaluate() .CreateBuilder() .Freeze() .IsValid() .Validate() could have some impact on consumer experiense, leaving them with the pure consumer features.
If possible: Getting rid of .EnumerateArray() in favour of some variant of IEnumerable and enabling implicit cast for all properties would have even more benefit.
@mwadams: It seems you put a lot of time, expertise and care into this project. Really enjoy it!! And I love to see source gens take over the old reflection monsters!
mwadams commentedon Aug 11, 2026
Now, I can provide you with
IEnumerablevery easily... I did in v4 but I wanted to see whether it was necessary in V5 before expanding the surface area again. However, you only get that on types that are pure arrays.mwadams commentedon Aug 11, 2026
@stadlaa - with regard to a POCO-like experience. I could easily do that by giving you an option to make those members internal.
mwadams commentedon Nov 2, 2026
Thanks for the discussion here. C# inheritance is not something we will add, because JSON Schema composes rather than inherits, but two of the suggestions are now issues of their own:
IEnumerable<T>(in progress)internalClosing this one in favour of those.