Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate OpenAPISchema objects from C# POCOs #836

Closed
darrelmiller opened this issue Apr 20, 2022 · 5 comments
Closed

Generate OpenAPISchema objects from C# POCOs #836

darrelmiller opened this issue Apr 20, 2022 · 5 comments
Assignees
Labels
status:on-hold An issue that we are yet to work on type:enhancement Enhancement request targeting an existing experience
Milestone

Comments

@darrelmiller
Copy link
Member

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

dotnet/aspnetcore#41246

@darrelmiller darrelmiller added the type:enhancement Enhancement request targeting an existing experience label Apr 20, 2022
@captainsafia
Copy link
Member

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

  • 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.
  • Schema generation is impacted by runtime behavior, particularly how JSON serializers are configured in the user application. For example, the user might leverage the JsonSerializerOptions.PropertyNamingPolicy to indicate whether property keys should be camel-cased or pascal-cased or insert-whatever-modification-here. The generator will need to be able to introspect these settings and respect them when generating property keys in the OpenAPI schema.
  • Users can also define custom JSON converters for a given type. For example, a Todo type that would typically serialized as an object can be overridden to serialize to just a string. The generator will need to respect these overrides.

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
Copy link
Member

JamesNK commented May 3, 2022

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
Copy link
Member Author

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
Copy link

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:on-hold An issue that we are yet to work on type:enhancement Enhancement request targeting an existing experience
Projects
None yet
Development

No branches or pull requests

6 participants