From 826628213695243448e739c761fdb43131b1a973 Mon Sep 17 00:00:00 2001 From: corneliu-petrescu <156085781+corneliu-petrescu@users.noreply.github.com> Date: Fri, 23 Aug 2024 11:52:39 +0300 Subject: [PATCH] fix: update sunset header (#368) * fix: update sunset header --- internal/compiler/compiler.go | 3 ++- internal/storage/collator.go | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index be4bbeb3..b7182639 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -294,7 +294,8 @@ var Versions embed.FS func (c *Compiler) BuildAll(ctx context.Context, stopVersion vervet.Version) error { var errs error for apiName := range c.apis { - err := c.Build(apiName, stopVersion) //nolint:contextcheck // TODO: fix contextcheck in separate PR + err := c.Build(apiName, stopVersion) //nolint:contextcheck + // TODO: fix contextcheck in separate PR if err != nil { errs = multierr.Append(errs, err) } diff --git a/internal/storage/collator.go b/internal/storage/collator.go index 824ff8d7..eef16fee 100644 --- a/internal/storage/collator.go +++ b/internal/storage/collator.go @@ -131,6 +131,10 @@ func (c *Collator) Collate() (map[vervet.Version]openapi3.T, error) { collatorMergeError.WithLabelValues(version.String()).Inc() return nil, err } + + // Overrides sunset header documentation until we can provide a more definitive fix + overrideSunsetHeader(spec) + if err := c.applyOverlay(spec); err != nil { log.Error().Err(err).Msgf("failed to merge overlay for version %s", version) collatorMergeError.WithLabelValues(version.String()).Inc() @@ -143,6 +147,45 @@ func (c *Collator) Collate() (map[vervet.Version]openapi3.T, error) { return specs, nil } +func overrideSunsetHeader(doc *openapi3.T) { + const headerDescription = "A header containing the date of when the underlying endpoint will be removed. " + + "This header is only present if the endpoint has been deprecated. " + + "For information purposes only. " + + "Returned as a date in the format: YYYY-MM-DD" + const example = "2021-08-02" + const schemaFormat = "date" + + for _, path := range doc.Paths.Map() { + for _, operation := range path.Operations() { + for _, responses := range operation.Responses.Map() { + if responses.Value == nil { + continue + } + + if sunsetHeader, ok := responses.Value.Headers["sunset"]; ok { + if sunsetHeader.Value == nil { + continue + } + sunsetHeader.Value.Description = headerDescription + sunsetHeader.Value.Example = example + if sunsetHeader.Value.Schema.Value == nil { + continue + } + sunsetHeader.Value.Schema.Value.Format = schemaFormat + } + } + } + } + if doc.Components != nil { + if sunsetHeader, ok := doc.Components.Headers["SunsetHeader"]; ok { + if sunsetHeader.Value == nil { + return + } + sunsetHeader.Value.Description = headerDescription + } + } +} + func mergeRevisions(revisions ContentRevisions) (*openapi3.T, error) { collator := vervet.NewCollator(vervet.StrictTags(false), vervet.UseFirstRoute(true)) var haveOpenAPI, haveOpenAPIVersion bool