Skip to content

Add support for CreateSchemaReferenceId option#56753

Merged
captainsafia merged 3 commits into
mainfrom
safia/schema-id-func
Jul 13, 2024
Merged

Add support for CreateSchemaReferenceId option#56753
captainsafia merged 3 commits into
mainfrom
safia/schema-id-func

Conversation

@captainsafia
Copy link
Copy Markdown
Contributor

Closes #56305.

This PR adds support for a CreateSchemaReferenceId property in the OpenApiOptions to support customizing how reference IDs are generated for schemas assocaited with a given type. Because of the behavior of the reference ID implementation, this function also serves as a mechanism for being able to determine whther or not a schema should be inline or referenced:

  • Providing a CreateSchemaReferenceId delegate that returns nullfor a given type will mean that schemas associated with that type are always inline
  • Providing a CreateSchemaReferenceId delegate that returns a value for a given type will mean that schemas assocaited with that type will always be referenced

Changes in this PR include:

  • Adding CreateSchemaReferenceId property to options object and CreateDefaultSchemaReferenceId static to provide users with access to the default implementation
  • Updates to invoke CreateSchemaReferenceId from options in all codepaths where it is currently relevant
  • New tests to cover reference ID behavior for scenarios we care about

@captainsafia captainsafia requested a review from a team as a code owner July 11, 2024 23:49
@ghost ghost added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jul 11, 2024
Comment thread src/OpenApi/src/Services/OpenApiOptions.cs Outdated
Comment thread src/OpenApi/src/Services/OpenApiOptions.cs Outdated
Co-authored-by: Martin Costello <martin@martincostello.com>
Comment thread src/OpenApi/test/Services/CreateSchemaReferenceIdTests.cs
schema.ApplyPrimitiveTypesAndFormats(context);
schema.ApplySchemaReferenceId(context);
schema.ApplyPolymorphismOptions(context);
var createSchemaReferenceId = optionsMonitor.Get(documentName).CreateSchemaReferenceId;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use _openApiOptions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_openApiOptions is an instance field so we can't use it in the initializer for _confguration. I could switch to standard constructors instead of primary constructors and initialize everything there but.... 😅 🤪

@captainsafia captainsafia merged commit 90a622d into main Jul 13, 2024
@captainsafia captainsafia deleted the safia/schema-id-func branch July 13, 2024 05:12
@dotnet-policy-service dotnet-policy-service Bot added this to the 9.0-preview7 milestone Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support options for setting callback used to generated reference IDs in OpenAPI

3 participants