-
Notifications
You must be signed in to change notification settings - Fork 67
feat: allow optional body parameter #773
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
src/Microsoft.OpenApi.OData.Reader/Common/RequestBodyRequirementAnalyzer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.OData.Edm; | ||
| using Microsoft.OpenApi.OData.Edm; | ||
| using Microsoft.OpenApi.OData.Vocabulary.Core; | ||
|
|
||
| namespace Microsoft.OpenApi.OData.Common | ||
| { | ||
| /// <summary> | ||
| /// Utility class for analyzing EDM types to determine if request bodies should be required. | ||
| /// </summary> | ||
| internal static class RequestBodyRequirementAnalyzer | ||
| { | ||
| /// <summary> | ||
| /// Determines if a request body should be required for an OData action. | ||
| /// </summary> | ||
| /// <param name="action">The EDM action.</param> | ||
| /// <returns>True if the request body should be required, false otherwise.</returns> | ||
| public static bool ShouldRequestBodyBeRequired(IEdmAction action) | ||
| { | ||
| if (action == null) | ||
| { | ||
| return true; // Safe default | ||
| } | ||
|
|
||
| // Get non-binding parameters | ||
| var parameters = action.IsBound | ||
| ? action.Parameters.Skip(1) | ||
| : action.Parameters; | ||
|
|
||
| // If no parameters, body is already null (existing behavior handles this) | ||
| if (!parameters.Any()) | ||
| { | ||
| return true; // Won't matter since body will be null | ||
| } | ||
|
|
||
| // Check if all parameters are nullable or optional | ||
| return !parameters.All(p => p.Type.IsNullable || p is IEdmOptionalParameter); | ||
|
gavinbarron marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines if a request body should be required for an entity or complex type. | ||
| /// </summary> | ||
| /// <param name="structuredType">The EDM structured type.</param> | ||
| /// <param name="isUpdateOperation">Whether this is an update operation (excludes key properties).</param> | ||
| /// <param name="model">The EDM model for additional context.</param> | ||
| /// <returns>True if the request body should be required, false otherwise.</returns> | ||
| public static bool ShouldRequestBodyBeRequired( | ||
| IEdmStructuredType structuredType, | ||
| bool isUpdateOperation, | ||
| IEdmModel? model = null) | ||
| { | ||
| if (structuredType == null) | ||
| { | ||
| return true; // Safe default | ||
| } | ||
|
|
||
| return !AreAllPropertiesOptional(structuredType, isUpdateOperation, model); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if all properties in a structured type are optional. | ||
| /// </summary> | ||
| /// <param name="structuredType">The EDM structured type.</param> | ||
| /// <param name="excludeKeyProperties">Whether to exclude key properties from analysis (for update operations).</param> | ||
| /// <param name="model">The EDM model for additional context.</param> | ||
| /// <returns>True if all properties are optional, false if any are required.</returns> | ||
| private static bool AreAllPropertiesOptional( | ||
| IEdmStructuredType structuredType, | ||
| bool excludeKeyProperties, | ||
| IEdmModel? model = null) | ||
| { | ||
| if (structuredType == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Collect all properties including inherited ones | ||
| var allProperties = new List<IEdmProperty>(); | ||
|
|
||
| // Get properties from current type and all base types | ||
| IEdmStructuredType currentType = structuredType; | ||
| while (currentType != null) | ||
| { | ||
| allProperties.AddRange(currentType.DeclaredStructuralProperties()); | ||
| allProperties.AddRange(currentType.DeclaredNavigationProperties()); | ||
| currentType = currentType.BaseType; | ||
| } | ||
|
|
||
| // If no properties, consider optional (empty body) | ||
| if (!allProperties.Any()) | ||
|
gavinbarron marked this conversation as resolved.
Outdated
|
||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Get key property names if we need to exclude them | ||
| HashSet<string>? keyNames = null; | ||
| if (excludeKeyProperties && structuredType is IEdmEntityType entityType) | ||
| { | ||
| keyNames = new HashSet<string>(entityType.Key().Select(k => k.Name)); | ||
|
gavinbarron marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // Check if ALL remaining properties are optional | ||
| foreach (var property in allProperties) | ||
| { | ||
| // Skip key properties if requested | ||
| if (keyNames != null && keyNames.Contains(property.Name)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Skip computed properties (read-only) | ||
| if (model != null && property is IEdmStructuralProperty && | ||
| (model.GetBoolean(property, CoreConstants.Computed) ?? false)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // If this property is required, the body must be required | ||
| if (!IsPropertyOptional(property, model)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if an individual property is optional. | ||
| /// </summary> | ||
| /// <param name="property">The EDM property.</param> | ||
| /// <param name="model">The EDM model for additional context.</param> | ||
| /// <returns>True if the property is optional, false if required.</returns> | ||
| private static bool IsPropertyOptional(IEdmProperty property, IEdmModel? model) | ||
|
Check warning on line 140 in src/Microsoft.OpenApi.OData.Reader/Common/RequestBodyRequirementAnalyzer.cs
|
||
|
gavinbarron marked this conversation as resolved.
Outdated
|
||
| { | ||
| if (property == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Structural properties (primitive, enum, complex) | ||
| if (property is IEdmStructuralProperty structuralProp) | ||
| { | ||
| // Has default value = optional | ||
| if (!string.IsNullOrEmpty(structuralProp.DefaultValueString)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Type is nullable = optional | ||
| if (structuralProp.Type.IsNullable) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Otherwise required | ||
| return false; | ||
| } | ||
|
|
||
| // Navigation properties | ||
| if (property is IEdmNavigationProperty navProp) | ||
| { | ||
| // Navigation properties are optional if nullable | ||
| return navProp.Type.IsNullable; | ||
| } | ||
|
|
||
| // Unknown property type, treat as required (safe default) | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.