-
Notifications
You must be signed in to change notification settings - Fork 14
POC: Merge allOf to identify how much OpenAPI size will change #557
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
Closed
Closed
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package filter | ||
|
||
import ( | ||
"github.com/getkin/kin-openapi/openapi3" | ||
) | ||
|
||
// MergeOneOfFilter: is a filter that merges properties from allOf into each child schema within oneOf to eliminate circular dependencies. | ||
type MergeOneOfFilter struct { | ||
oas *openapi3.T | ||
} | ||
|
||
func (*MergeOneOfFilter) ValidateMetadata() error { | ||
return nil | ||
} | ||
|
||
func (f *MergeOneOfFilter) Apply() error { | ||
if f.oas == nil { | ||
return nil | ||
} | ||
|
||
// Process components schemas | ||
if f.oas.Components != nil && f.oas.Components.Schemas != nil { | ||
for _, schemaRef := range f.oas.Components.Schemas { | ||
if schemaRef != nil && schemaRef.Value != nil { | ||
f.processSchema(schemaRef.Value) | ||
} | ||
} | ||
} | ||
|
||
// Process paths and their operations | ||
if f.oas.Paths != nil { | ||
for _, pathItem := range f.oas.Paths.Map() { | ||
// Process parameters at path level | ||
for _, paramRef := range pathItem.Parameters { | ||
if paramRef != nil && paramRef.Value != nil && paramRef.Value.Schema != nil && paramRef.Value.Schema.Value != nil { | ||
f.processSchema(paramRef.Value.Schema.Value) | ||
} | ||
} | ||
|
||
// Process operations | ||
for _, operation := range pathItem.Operations() { | ||
// Process operation parameters | ||
for _, paramRef := range operation.Parameters { | ||
if paramRef != nil && paramRef.Value != nil && paramRef.Value.Schema != nil && paramRef.Value.Schema.Value != nil { | ||
f.processSchema(paramRef.Value.Schema.Value) | ||
} | ||
} | ||
|
||
// Process request body | ||
if operation.RequestBody != nil && operation.RequestBody.Value != nil { | ||
for _, mediaType := range operation.RequestBody.Value.Content { | ||
if mediaType.Schema != nil && mediaType.Schema.Value != nil { | ||
f.processSchema(mediaType.Schema.Value) | ||
} | ||
} | ||
} | ||
|
||
// Process responses | ||
for _, responseRef := range operation.Responses.Map() { | ||
if responseRef != nil && responseRef.Value != nil { | ||
for _, mediaType := range responseRef.Value.Content { | ||
if mediaType.Schema != nil && mediaType.Schema.Value != nil { | ||
f.processSchema(mediaType.Schema.Value) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// processSchema recursively processes a schema to merge allOf properties into oneOf schemas | ||
func (f *MergeOneOfFilter) processSchema(schema *openapi3.Schema) { | ||
if schema == nil { | ||
return | ||
} | ||
|
||
// If schema has both allOf and oneOf, we need to merge properties | ||
if len(schema.AllOf) > 0 && len(schema.OneOf) > 0 { | ||
// Collect all properties from allOf schemas | ||
allOfProperties := make(map[string]*openapi3.SchemaRef) | ||
allOfRequired := make([]string, 0) | ||
|
||
for _, allOfSchema := range schema.AllOf { | ||
if allOfSchema != nil && allOfSchema.Value != nil { | ||
// Merge properties | ||
for propName, propSchema := range allOfSchema.Value.Properties { | ||
allOfProperties[propName] = propSchema | ||
} | ||
|
||
// Merge required fields | ||
allOfRequired = append(allOfRequired, allOfSchema.Value.Required...) | ||
} | ||
} | ||
|
||
// Apply collected properties to each oneOf schema | ||
for _, oneOfSchema := range schema.OneOf { | ||
if oneOfSchema != nil && oneOfSchema.Value != nil { | ||
// Initialize properties map if nil | ||
if oneOfSchema.Value.Properties == nil { | ||
oneOfSchema.Value.Properties = make(map[string]*openapi3.SchemaRef) | ||
} | ||
|
||
// Copy allOf properties into oneOf schema | ||
for propName, propSchema := range allOfProperties { | ||
// Only add if the property doesn't exist in the oneOf schema | ||
if _, exists := oneOfSchema.Value.Properties[propName]; !exists { | ||
oneOfSchema.Value.Properties[propName] = propSchema | ||
} | ||
} | ||
|
||
// Merge required fields (avoiding duplicates) | ||
requiredSet := make(map[string]bool) | ||
for _, req := range oneOfSchema.Value.Required { | ||
requiredSet[req] = true | ||
} | ||
|
||
for _, req := range allOfRequired { | ||
if !requiredSet[req] { | ||
oneOfSchema.Value.Required = append(oneOfSchema.Value.Required, req) | ||
requiredSet[req] = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Recursively process nested schemas | ||
|
||
// Process properties | ||
for _, propSchema := range schema.Properties { | ||
if propSchema != nil && propSchema.Value != nil { | ||
f.processSchema(propSchema.Value) | ||
} | ||
} | ||
|
||
// Process additionalProperties | ||
if schema.AdditionalProperties.Schema != nil && schema.AdditionalProperties.Schema.Value != nil { | ||
f.processSchema(schema.AdditionalProperties.Schema.Value) | ||
} | ||
|
||
// Process items for arrays | ||
if schema.Items != nil && schema.Items.Value != nil { | ||
f.processSchema(schema.Items.Value) | ||
} | ||
|
||
// Process allOf schemas | ||
for _, subSchema := range schema.AllOf { | ||
if subSchema != nil && subSchema.Value != nil { | ||
f.processSchema(subSchema.Value) | ||
} | ||
} | ||
|
||
// Process oneOf schemas | ||
for _, subSchema := range schema.OneOf { | ||
if subSchema != nil && subSchema.Value != nil { | ||
f.processSchema(subSchema.Value) | ||
} | ||
} | ||
|
||
// Process anyOf schemas | ||
for _, subSchema := range schema.AnyOf { | ||
if subSchema != nil && subSchema.Value != nil { | ||
f.processSchema(subSchema.Value) | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andreaangiolillo low priority question. Do you have any proven way to run cli for testing with our openapi?
Any gist/documentation that does run CLI?