Skip to content

Commit 2da39aa

Browse files
author
bnasslahsen
committed
éWeird YAML tag in section OpenAPI spec file. Fixes #774
1 parent 76b9c18 commit 2da39aa

File tree

9 files changed

+181
-4
lines changed

9 files changed

+181
-4
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

+16
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@
4040
import java.util.stream.Collectors;
4141

4242
import com.fasterxml.jackson.annotation.JsonView;
43+
import com.fasterxml.jackson.databind.ObjectMapper;
44+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
45+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
4346
import io.swagger.v3.core.filter.SpecFilter;
4447
import io.swagger.v3.core.util.ReflectionUtils;
48+
import io.swagger.v3.core.util.Yaml;
4549
import io.swagger.v3.oas.annotations.Hidden;
4650
import io.swagger.v3.oas.annotations.callbacks.Callback;
4751
import io.swagger.v3.oas.annotations.enums.ParameterIn;
@@ -953,4 +957,16 @@ protected void initOpenAPIBuilder() {
953957
openAPIBuilder = openAPIBuilderObjectFactory.getObject();
954958
}
955959
}
960+
961+
/**
962+
* Gets yaml mapper.
963+
*
964+
* @return the yaml mapper
965+
*/
966+
protected ObjectMapper getYamlMapper() {
967+
ObjectMapper objectMapper = Yaml.mapper();
968+
YAMLFactory factory = (YAMLFactory) objectMapper.getFactory();
969+
factory.configure(Feature.USE_NATIVE_TYPE_ID, false);
970+
return objectMapper;
971+
}
956972
}

springdoc-openapi-webflux-core/src/main/java/org/springdoc/webflux/api/OpenApiResource.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Set;
2929

3030
import com.fasterxml.jackson.core.JsonProcessingException;
31+
import com.fasterxml.jackson.databind.ObjectMapper;
3132
import io.swagger.v3.core.util.Json;
3233
import io.swagger.v3.core.util.PathUtils;
3334
import io.swagger.v3.core.util.Yaml;
@@ -164,9 +165,9 @@ public Mono<String> openapiYaml(ServerHttpRequest serverHttpRequest,
164165
calculateServerUrl(serverHttpRequest, apiDocsUrl);
165166
OpenAPI openAPI = this.getOpenApi();
166167
if (!springDocConfigProperties.isWriterWithDefaultPrettyPrinter())
167-
return Mono.just(Yaml.mapper().writeValueAsString(openAPI));
168+
return Mono.just(getYamlMapper().writeValueAsString(openAPI));
168169
else
169-
return Mono.just(Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI));
170+
return Mono.just(getYamlMapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI));
170171
}
171172

172173
@Override

springdoc-openapi-webmvc-core/src/main/java/org/springdoc/webmvc/api/OpenApiResource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ public String openapiYaml(HttpServletRequest request, @Value(DEFAULT_API_DOCS_UR
199199
calculateServerUrl(request, apiDocsUrl);
200200
OpenAPI openAPI = this.getOpenApi();
201201
if (!springDocConfigProperties.isWriterWithDefaultPrettyPrinter())
202-
return Yaml.mapper().writeValueAsString(openAPI);
202+
return getYamlMapper().writeValueAsString(openAPI);
203203
else
204-
return Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);
204+
return getYamlMapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);
205205
}
206206

207207
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test.org.springdoc.api.app127;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
6+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
7+
include = JsonTypeInfo.As.EXISTING_PROPERTY,
8+
property = "type",
9+
visible = true
10+
)
11+
@JsonSubTypes({
12+
@JsonSubTypes.Type(value = ConcreteObjectA.class, name = "Type A")
13+
})
14+
public abstract class AbstractObject {
15+
16+
private final ConcreteType type;
17+
private final String name;
18+
19+
protected AbstractObject(ConcreteType type, String name) {
20+
this.type = type;
21+
this.name = name;
22+
}
23+
24+
public ConcreteType getType() {
25+
return type;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test.org.springdoc.api.app127;
2+
3+
public class ConcreteObjectA extends AbstractObject {
4+
5+
private final String description;
6+
7+
public ConcreteObjectA(String name, String description) {
8+
super(ConcreteType.TYPE_A, name);
9+
this.description = description;
10+
}
11+
12+
public String getDescription() {
13+
return description;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package test.org.springdoc.api.app127;
2+
3+
public enum ConcreteType {
4+
TYPE_A("Type A"),
5+
TYPE_B("Type B");
6+
7+
private final String name;
8+
9+
ConcreteType(String name) {
10+
this.name = name;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.org.springdoc.api.app127;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.media.Content;
5+
import io.swagger.v3.oas.annotations.media.ExampleObject;
6+
import io.swagger.v3.oas.annotations.media.Schema;
7+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
8+
9+
import org.springframework.http.MediaType;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
@RestController
14+
public class Controller {
15+
16+
@Operation(summary = "Test Bug", responses = {
17+
@ApiResponse(responseCode = "200", description = "OK",
18+
content = @Content(
19+
schema = @Schema(implementation = Umbrella.class),
20+
examples = @ExampleObject(ref = "#/components/examples/umbrellaExample", name = "Example with weird YAML tag")
21+
)
22+
)
23+
})
24+
@GetMapping(value = "/bug", produces = MediaType.APPLICATION_JSON_VALUE)
25+
public Umbrella bug() {
26+
return new Umbrella(new ConcreteObjectA("a", "b"));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
package test.org.springdoc.api.app127;
24+
25+
import org.junit.Assert;
26+
import org.junit.jupiter.api.Test;
27+
import org.springdoc.core.Constants;
28+
import test.org.springdoc.api.AbstractSpringDocTest;
29+
30+
import org.springframework.boot.autoconfigure.SpringBootApplication;
31+
import org.springframework.test.web.servlet.MvcResult;
32+
33+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
34+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
35+
36+
37+
/**
38+
* Tests Spring meta-annotations as method parameters
39+
*/
40+
public class SpringDocApp127Test extends AbstractSpringDocTest {
41+
42+
@SpringBootApplication
43+
static class SpringDocTestApp {}
44+
45+
@Test
46+
public void testApp() throws Exception {
47+
className = getClass().getSimpleName();
48+
String testNumber = className.replaceAll("[^0-9]", "");
49+
MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL+".yaml")).andExpect(status().isOk()).andReturn();
50+
String result = mockMvcResult.getResponse().getContentAsString();
51+
Assert.assertTrue(!result.contains("!<Type A>"));
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test.org.springdoc.api.app127;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
public class Umbrella {
6+
7+
@Schema(description = "This reference to abstract class causes weird YAML tag to be added", anyOf = ConcreteObjectA.class)
8+
private final AbstractObject object;
9+
10+
public Umbrella(AbstractObject object) {
11+
this.object = object;
12+
}
13+
14+
public AbstractObject getObject() {
15+
return object;
16+
}
17+
}

0 commit comments

Comments
 (0)