Skip to content

Commit

Permalink
Fix wrong merge operations with different groups (micronaut-projects#…
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Apr 18, 2024
1 parent b8315a0 commit a87da55
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ Map<String, List<PathItem>> resolvePathItems(VisitorContext context, List<UriMat

var result = new StringBuilder();

boolean optionalPathVar = false;
boolean varProcess = false;
boolean valueProcess = false;
boolean isFirstVarChar = true;
Expand Down Expand Up @@ -452,19 +451,6 @@ Map<String, List<PathItem>> resolvePathItems(VisitorContext context, List<UriMat
return resultPathItemsMap;
}

private List<String> addOptionalVars(List<String> paths, String var, int level) {
var additionalPaths = new ArrayList<>(paths);
if (paths.isEmpty()) {
additionalPaths.add(SLASH + OPEN_BRACE + var + CLOSE_BRACE);
return additionalPaths;
}

for (String path : paths) {
additionalPaths.add(path + SLASH + OPEN_BRACE + var + CLOSE_BRACE);
}
return additionalPaths;
}

/**
* Convert the values to a map.
*
Expand Down Expand Up @@ -1389,7 +1375,6 @@ protected Schema<?> bindSchemaForElement(VisitorContext context, TypedElement el
arraySchemaAnn.stringValue(PROP_NAME).ifPresent(schemaToBind::setName);
}

// Schema finalSchemaToBind = schemaToBind;
processJakartaValidationAnnotations(element, elementType, schemaToBind);

final ComposedSchema composedSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ private void mergeMicronautEndpointInfos(OpenAPI openApi, VisitorContext context
setOperationOnPathItem(pathItem, endpointInfo.getHttpMethod(), endpointInfo.getOperation());
continue;
}
if (endpointInfo.getVersion() == null) {
if (endpointInfo.getVersion() == null && CollectionUtils.isEmpty(endpointInfo.getGroups())) {
setOperationOnPathItem(pathItem, endpointInfo.getHttpMethod(), SchemaUtils.mergeOperations(operation, endpointInfo.getOperation()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,138 @@ public class MyBean {}
apiPrivate.paths.size() == 1
apiPublic.paths.size() == 2
}

void "test group schemas with exclude"() {

when:
buildBeanDefinition("test.MyBean", '''
package test;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.openapi.annotation.OpenAPIGroup;
import io.micronaut.serde.annotation.Serdeable;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
@Controller("/demo")
class MyController {
@OpenAPIGroup(names = "v1", exclude = "v2")
@Get
HelloResponseV1 indexV1(String id) {
return null;
}
@OpenAPIGroup(names = "v2", exclude = "v1")
@Get
HelloResponseV2 indexV2(String name) {
return null;
}
}
@Serdeable.Serializable
class HelloResponseV1 {
public String message;
}
@Serdeable.Serializable
class HelloResponseV2 {
public String message;
}
@OpenAPIDefinition(
info = @Info(
title = "Title My API",
version = "0.0",
description = "My API"
)
)
class Application {
}
@jakarta.inject.Singleton
public class MyBean {}
''')

then:
def openApis = Utils.testReferences
openApis
openApis.size() == 2

def apiV1 = openApis.get(Pair.of("v1", null)).getOpenApi()
def apiV2 = openApis.get(Pair.of("v2", null)).getOpenApi()

apiV1.paths.'/demo'.get.responses.'200'.content.'application/json'.schema.$ref == '#/components/schemas/HelloResponseV1'
apiV2.paths.'/demo'.get.responses.'200'.content.'application/json'.schema.$ref == '#/components/schemas/HelloResponseV2'
}

void "test group schemas without exclude"() {

when:
buildBeanDefinition("test.MyBean", '''
package test;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.openapi.annotation.OpenAPIGroup;
import io.micronaut.serde.annotation.Serdeable;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
@Controller("/demo")
class MyController {
@OpenAPIGroup("v1")
@Get
HelloResponseV1 indexV1(String id) {
return null;
}
@OpenAPIGroup("v2")
@Get
HelloResponseV2 indexV2(String name) {
return null;
}
}
@Serdeable.Serializable
class HelloResponseV1 {
public String message;
}
@Serdeable.Serializable
class HelloResponseV2 {
public String message;
}
@OpenAPIDefinition(
info = @Info(
title = "Title My API",
version = "0.0",
description = "My API"
)
)
class Application {
}
@jakarta.inject.Singleton
public class MyBean {}
''')

then:
def openApis = Utils.testReferences
openApis
openApis.size() == 2

def apiV1 = openApis.get(Pair.of("v1", null)).getOpenApi()
def apiV2 = openApis.get(Pair.of("v2", null)).getOpenApi()

apiV1.paths.'/demo'.get.responses.'200'.content.'application/json'.schema.$ref == '#/components/schemas/HelloResponseV1'
apiV2.paths.'/demo'.get.responses.'200'.content.'application/json'.schema.$ref == '#/components/schemas/HelloResponseV2'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.micronaut.openapi.visitor
import io.micronaut.openapi.AbstractOpenApiTypeElementSpec
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation
import spock.util.environment.RestoreSystemProperties

class OpenApiVersionSpec extends AbstractOpenApiTypeElementSpec {

Expand Down Expand Up @@ -356,4 +357,76 @@ class MyBean {}
System.clearProperty("micronaut.router.versioning.enabled")
System.clearProperty("micronaut.router.versioning.parameter.enabled")
}

@RestoreSystemProperties
void "test group schemas with version"() {

setup:
System.setProperty("micronaut.router.versioning.enabled", "true")
System.setProperty("micronaut.router.versioning.parameter.enabled", "true")

when:
buildBeanDefinition("test.MyBean", '''
package test;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.serde.annotation.Serdeable;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
@Controller("/demo")
class MyController {
@Version("v1")
@Get
HelloResponseV1 indexV1(String id) {
return null;
}
@Version("v2")
@Get
HelloResponseV2 indexV2(String name) {
return null;
}
}
@Serdeable.Serializable
class HelloResponseV1 {
public String message;
}
@Serdeable.Serializable
class HelloResponseV2 {
public String message;
}
@OpenAPIDefinition(
info = @Info(
title = "Title My API",
version = "0.0",
description = "My API"
)
)
class Application {
}
@jakarta.inject.Singleton
public class MyBean {}
''')

then:
def openApis = Utils.testReferences
openApis
openApis.size() == 2

def apiV1 = openApis.get(Pair.of(null, "v1")).getOpenApi()
def apiV2 = openApis.get(Pair.of(null, "v2")).getOpenApi()

apiV1.paths.'/demo'.get.responses.'200'.content.'application/json'.schema.$ref == '#/components/schemas/HelloResponseV1'
apiV2.paths.'/demo'.get.responses.'200'.content.'application/json'.schema.$ref == '#/components/schemas/HelloResponseV2'
}
}

0 comments on commit a87da55

Please sign in to comment.