| 
 | 1 | +// Copyright 2025 MongoDB Inc  | 
 | 2 | +//  | 
 | 3 | +// Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 4 | +// you may not use this file except in compliance with the License.  | 
 | 5 | +// You may obtain a copy of the License at  | 
 | 6 | +//  | 
 | 7 | +//	http://www.apache.org/licenses/LICENSE-2.0  | 
 | 8 | +//  | 
 | 9 | +// Unless required by applicable law or agreed to in writing, software  | 
 | 10 | +// distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 12 | +// See the License for the specific language governing permissions and  | 
 | 13 | +// limitations under the License.  | 
 | 14 | + | 
 | 15 | +package filter  | 
 | 16 | + | 
 | 17 | +import (  | 
 | 18 | +	"strings"  | 
 | 19 | + | 
 | 20 | +	"github.com/getkin/kin-openapi/openapi3"  | 
 | 21 | +)  | 
 | 22 | + | 
 | 23 | +const sunsetToBeDecided = "9999-12-31"  | 
 | 24 | + | 
 | 25 | +// SunsetFilter removes the sunsetToBeDecided from the openapi specification.  | 
 | 26 | +type SunsetFilter struct {  | 
 | 27 | +	oas *openapi3.T  | 
 | 28 | +}  | 
 | 29 | + | 
 | 30 | +func (*SunsetFilter) ValidateMetadata() error {  | 
 | 31 | +	return nil  | 
 | 32 | +}  | 
 | 33 | + | 
 | 34 | +func (f *SunsetFilter) Apply() error {  | 
 | 35 | +	if f.oas.Paths == nil {  | 
 | 36 | +		return nil  | 
 | 37 | +	}  | 
 | 38 | + | 
 | 39 | +	for _, pathItem := range f.oas.Paths.Map() {  | 
 | 40 | +		for _, operation := range pathItem.Operations() {  | 
 | 41 | +			if operation.Responses == nil {  | 
 | 42 | +				continue  | 
 | 43 | +			}  | 
 | 44 | + | 
 | 45 | +			applyOnOperation(operation)  | 
 | 46 | +		}  | 
 | 47 | +	}  | 
 | 48 | +	return nil  | 
 | 49 | +}  | 
 | 50 | + | 
 | 51 | +func applyOnOperation(op *openapi3.Operation) {  | 
 | 52 | +	for key, response := range op.Responses.Map() {  | 
 | 53 | +		if !strings.HasPrefix(key, "20") {  | 
 | 54 | +			continue  | 
 | 55 | +		}  | 
 | 56 | + | 
 | 57 | +		for _, content := range response.Value.Content {  | 
 | 58 | +			if v, ok := content.Extensions["x-sunset"]; ok {  | 
 | 59 | +				if v != sunsetToBeDecided {  | 
 | 60 | +					continue  | 
 | 61 | +				}  | 
 | 62 | +				delete(content.Extensions, "x-sunset")  | 
 | 63 | +			}  | 
 | 64 | +		}  | 
 | 65 | +	}  | 
 | 66 | +}  | 
0 commit comments