Skip to content

Commit

Permalink
fix(swagger): ensure byteArrayHttpMessageConverter is the first conve…
Browse files Browse the repository at this point in the history
…rter to render swagger UI (spinnaker#1865)

* fix(swagger): ensure byteArrayHttpMessageConverter is the first converter to render swagger UI

* test(swagger): add test for swagger openapi migration

* test(swagger): refactor fix and rename swagger test
  • Loading branch information
edgarulg authored Feb 11, 2025
1 parent f2528f0 commit aaf0414
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Expand All @@ -27,6 +28,11 @@ public class CloudEventHandlerConfiguration implements WebMvcConfigurer {

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(
0,
new ByteArrayHttpMessageConverter()); // adding ByteArrayHttpMessageConverter as the first
// element to avoid Swagger decode issues. See:
// https://github.com/springdoc/springdoc-openapi/issues/2143
converters.add(cloudEventHttpMessageConverter());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2025 Harness, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.config;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.netflix.spinnaker.gate.Main;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;

@AutoConfigureMockMvc
@SpringBootTest(classes = Main.class)
@ActiveProfiles("swaggertest")
@TestPropertySource(properties = {"spring.config.location=classpath:gate-test.yml"})
public class GateSwaggerConfigTest {

@Autowired private MockMvc mockMvc;

private static final String OPENAPI_API_PATH = "/v3/api-docs";

@Test
void TestSwaggerDocsIsNotMalformed() throws Exception {
mockMvc
.perform(get(OPENAPI_API_PATH))
.andDo(print())
.andExpect(status().isOk())
.andExpect(
jsonPath("$.openapi", startsWith("3."))) // validates we use version 3.x.x from kork
.andExpect(jsonPath("$.info.title", is("Spinnaker Test")));
}
}
15 changes: 15 additions & 0 deletions gate-web/src/test/resources/gate-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ spinnaker:
front50:
enabled: true
url: https://front50.net

---

spring:
config:
activate:
on-profile: swaggertest

swagger:
enabled: true
title: Spinnaker Test
description:
contact:
patterns:
- /test

0 comments on commit aaf0414

Please sign in to comment.