Skip to content
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

Fix error in OpenAPI handling of default values' types #5289

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,10 @@
<artifactId>helidon-config-yaml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -196,8 +196,12 @@ Class<?> impl() {
return impl;
}

boolean hasDefaultProperty() {
return getPropertyNoEx("defaultValue") != null;
/**
*
* @return the 'default' property for this type; null if none
*/
Property defaultProperty() {
return getPropertyNoEx("defaultValue");
}

private static boolean setupExtensionType(String key, Node valueNode) {
Expand Down
6 changes: 4 additions & 2 deletions openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.eclipse.microprofile.openapi.models.servers.ServerVariable;
import org.jboss.jandex.IndexView;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.introspector.Property;

import static io.helidon.webserver.cors.CorsEnabledServiceHelper.CORS_CONFIG_KEY;

Expand Down Expand Up @@ -273,8 +274,9 @@ private static void adjustTypeDescriptions(Map<Class<?>, ExpandedTypeDescription
if (Extensible.class.isAssignableFrom(td.getType())) {
td.addExtensions();
}
if (td.hasDefaultProperty()) {
td.substituteProperty("default", Object.class, "getDefaultValue", "setDefaultValue");
Property defaultProperty = td.defaultProperty();
if (defaultProperty != null) {
td.substituteProperty("default", defaultProperty.getType(), "getDefaultValue", "setDefaultValue");
td.addExcludes("defaultValue");
}
if (isRef(td)) {
Expand Down
25 changes: 24 additions & 1 deletion openapi/src/test/java/io/helidon/openapi/ParserTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,10 +19,15 @@
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import io.helidon.config.testing.OptionalMatcher;

import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.Paths;
import org.eclipse.microprofile.openapi.models.parameters.Parameter;
import org.eclipse.microprofile.openapi.models.servers.Server;
import org.eclipse.microprofile.openapi.models.servers.ServerVariable;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -125,4 +130,22 @@ static OpenAPI parse(SnakeYAMLParserHelper<ExpandedTypeDescription> helper, Stri
return OpenAPIParser.parse(helper.types(), is, mediaType);
}
}

@Test
void testComplicatedPetstoreDocument() throws IOException {
OpenAPI openAPI = parse(helper,"/petstore-with-fake-endpoints-models-for-testing.yaml",
OpenAPISupport.OpenAPIMediaType.YAML);
assertThat(openAPI.getOpenapi(), is("3.0.0"));
assertThat("Default for server variable 'port'",
openAPI.getPaths()
.getPathItem("/pet")
.getServers()
.stream()
.filter(server -> server.getUrl().equals("http://{server}.swagger.io:{port}/v2"))
.map(Server::getVariables)
.map(map -> map.get("server"))
.map(ServerVariable::getDefaultValue)
.findFirst(),
OptionalMatcher.value(is("petstore")));
}
}
Loading