Skip to content

Commit

Permalink
2.x: Fix error in OpenAPI handling of default values' types (#7642)
Browse files Browse the repository at this point in the history
  • Loading branch information
Captain1653 committed Jan 24, 2025
1 parent 9a9640b commit 288c9aa
Show file tree
Hide file tree
Showing 4 changed files with 1,990 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 2025 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 @@ -186,8 +186,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
8 changes: 5 additions & 3 deletions openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
* Copyright (c) 2020, 2025 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 @@ -82,6 +82,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 org.yaml.snakeyaml.introspector.PropertySubstitute;

import static io.helidon.webserver.cors.CorsEnabledServiceHelper.CORS_CONFIG_KEY;
Expand Down Expand Up @@ -307,8 +308,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
23 changes: 22 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, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2025 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 @@ -20,9 +20,12 @@
import java.util.List;
import java.util.Map;

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;

Expand Down Expand Up @@ -119,6 +122,24 @@ public void testParserUsingJSON() throws IOException {
// containsString("needs to be added to the store"));
}

@Test
void testComplicatedPetstoreDocument() throws IOException {
OpenAPI openAPI = parse(helper,"/petstore-with-fake-endpoints-models-for-testing.yml",
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")));
}

static OpenAPI parse(SnakeYAMLParserHelper<ExpandedTypeDescription> helper, String path,
OpenAPISupport.OpenAPIMediaType mediaType) throws IOException {
try (InputStream is = ParserTest.class.getResourceAsStream(path)) {
Expand Down
Loading

0 comments on commit 288c9aa

Please sign in to comment.