dotnet-operator-sdk icon indicating copy to clipboard operation
dotnet-operator-sdk copied to clipboard

[feature]: JsonSchema.Net.Generation for openapi schema generation?

Open SGStino opened this issue 1 year ago • 8 comments

Is your feature request related to a problem? Please describe.

Currently, the schema generation is limited to what is implemented,

Issues like https://github.com/buehler/dotnet-operator-sdk/issues/741, https://github.com/buehler/dotnet-operator-sdk/issues/396, https://github.com/buehler/dotnet-operator-sdk/issues/351 could possibly benefit from this

Describe the solution you would like

It would be nice to team up with a schema generator library and outsource those capabilities to generate the OpenApiV3 spec.

JsonSchema.Net offers JsonSchema with customizable keywords and validation on JsonNodes. JsonSchema.Net.Generation can generate schemas from C# classes with reflection (for use in KubeOps.Cli?)

Additional Context

No response

SGStino avatar Apr 17 '24 09:04 SGStino

Hey @SGStino

That's a good idea! As long as there are possibilities to add the special "x-kubernetes" attributes, we should be good to go. According to the docs of Kubernetes, they use openapi v3 and as such, we could use a lib for that.

buehler avatar Apr 18 '24 08:04 buehler

The library supports adding custom vocabulary, so the x-kubernetes should be implemented as those.

I'll see if I can bash something together as reference so you can see how to fit it in the operator?

SGStino avatar Apr 19 '24 08:04 SGStino

That would be amazing! Thank you :-)

buehler avatar Apr 19 '24 08:04 buehler

https://github.com/SGStino/JsonSchemaTest

If you have any real-life examples of entities we can test against? Just to see what comes out, then we can figure out if anything works different or not at all.

SGStino avatar Apr 21 '24 09:04 SGStino

Hey @SGStino Thanks :-)

Sadly - no. Since I made this SDK, I've only written one operator (weird, I know).

But maybe other contributors can provide some real world examples.

buehler avatar Apr 24 '24 07:04 buehler

New stuff that might change the approach:

Maps .NET types to JSON schema objects using contract metadata from JsonTypeInfo instances.

https://learn.microsoft.com/en-us/dotnet/api/system.text.json.schema.jsonschemaexporter?view=net-9.0

@buehler @SGStino

wasabii avatar Jan 06 '25 03:01 wasabii

That might explain the demise of swashbuckle in dotnet 9. No more reflection, only source generated JsonTypeInfo instances. although i don't immediatly see how the x-kubernetes- ... things might fit into the type info.

Depending on how extendable the generator is in it's early days it could be easier to use json-everything. As it should in theory come up with the same schema, but with the extensibility for the x-... attrubtes?s

SGStino avatar Jan 13 '25 18:01 SGStino

The JSON Schema Exporter stuff is consumed by https://github.com/microsoft/OpenAPI.NET and https://www.nuget.org/packages/Microsoft.AspNetCore.OpenApi to do the full picture. My belief so far is there are ample extension points to do the whole stack.

For instance, ASP.NET allows schema modification by various attributes and stuff. And schema document transformation, etc. So you can pretty much make it look how you want.

The ASP.NET Core version of the usage build schema off of the API Explorer API, which exports Action/Type information, etc. Basically tying a few different libraries together.

wasabii avatar Jan 13 '25 18:01 wasabii