microsoft / OpenAPI.NET

The OpenAPI.NET SDK contains a useful object model for OpenAPI documents in .NET along with common serializers to extract raw OpenAPI JSON and YAML documents from the model.
MIT License
1.38k stars 231 forks source link

Generate OpenAPISchema objects from C# POCOs #836

Open darrelmiller opened 2 years ago

darrelmiller commented 2 years ago

Can we create a mechanism to generate Schemas from C# classes?

https://github.com/dotnet/aspnetcore/issues/41246

captainsafia commented 2 years ago

@JamesNK and I talked schema generation today and he shared some helpful insights on this domain. Notes from the discussion below:

We also discussed some features that might be helpful for the work that James is doing around gRPC + REST APIs + OpenAPI, particularly being able to provide global overrides for how different JSON schema types (primitives, objects, arrays, etc.) should have their serializations configured.

Let me know if I missed anything key, @JamesNK...

JamesNK commented 2 years ago

In the context of OpenAPI, schemas exist outside the scope of a particular operation. The same Type/Schema can be used in different operations. As we reason through this, we should consider that the schema generation will need to happen independent of the operation model.

Another key thing here is schemas aren't isolated. Schemas are reused and they reference each other. OpenApiSchema SchemaGenerator.Generate(Type type) isn't sufficient.

For this to work I imagine:

  1. You would need a higher-level view of a model, which has a collection of System.Type instances an operation will use (in and out).
  2. These types are then registered with a schema generator.
  3. Once all types are registered then generation happens. Internally the generation will probably have mapping between type and generated model, e.g. Dictionary<Type, OpenApiSchema>.
  4. Something then instructs the schema generate to adds the generated schemas to the OpenAPI doc (is components.schemas a standard place?) and then goes through the operations and adds references to the schemas in components.schemas based on the type.
darrelmiller commented 1 year ago

Our plans to support OpenAPI 3.1 currently involve changing our OpenAPISchema object to use a third-party JSONSchema library. We should not do this work until we have added OpenAPI 3.1 support or we will have to do the work twice.

magarich228 commented 1 month ago

Hello, I am at the stage of selecting the technology to work with OpenAPI standard required for my task. I need functionality to generate schemas. If possible, please advise how long this issue will be on hold?

captainsafia commented 3 weeks ago

I think we can close this issue out given support. for generating schemas from type definitions has been added in System.Text.Json.

@magarich228 There is a new API in .NET 9 to support this (JsonSchemaExporter). Alternatively, you can take a look at the Microsoft.AspNetCore.OpenApi package v9 to see if that meets your needs. It supports generating OpenAPI documents for API implementations.