diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/InlineModelResolverTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/InlineModelResolverTest.java index 52350b121f6..4187592dcca 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/InlineModelResolverTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/InlineModelResolverTest.java @@ -17,6 +17,7 @@ package org.openapitools.codegen; +import io.swagger.parser.OpenAPIParser; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; @@ -25,6 +26,7 @@ import io.swagger.v3.oas.models.media.*; import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; +import io.swagger.v3.parser.core.models.ParseOptions; import org.openapitools.codegen.utils.ModelUtils; import org.testng.annotations.Test; @@ -465,6 +467,62 @@ public class InlineModelResolverTest { assertTrue(additionalProperties.getProperties().get("resolve_inline_object_response_with_additional_properties") instanceof StringSchema); } + @Test + public void resolveInlineMapSchemaInResponse() { + OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI(); + ApiResponse apiResponse = openAPI + .getPaths() + .get("/resolve_inline_map_schema_in_response") + .getGet() + .getResponses() + .get("200"); + + // NOTE: Swagger parser doesn't use MapSchema currently, + // so we need to set a MapSchema instance as the schema manually for testing. + // @see https://github.com/swagger-api/swagger-parser/blob/master/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/SchemaTypeUtil.java + apiResponse.content( + new Content().addMediaType( + "application/json", + new MediaType().schema( + new MapSchema().additionalProperties( + new ObjectSchema().addProperties( + "resolve_inline_map_schema_in_response_property", + new ObjectSchema().addProperties( + "resolve_inline_map_schema_in_response_property_string", + new StringSchema().example("example") + ) + ) + ) + ) + ) + ); + + new InlineModelResolver().flatten(openAPI); + + MediaType mediaType = openAPI + .getPaths() + .get("/resolve_inline_map_schema_in_response") + .getGet() + .getResponses() + .get("200") + .getContent() + .get("application/json"); + assertTrue(mediaType.getSchema() instanceof MapSchema); + + Schema additionalProperties = (Schema) mediaType.getSchema().getAdditionalProperties(); + assertNotNull(additionalProperties.get$ref()); + assertTrue(additionalProperties.get$ref().startsWith("#/components/schemas/inline_response_")); + + Schema referencedSchema = ModelUtils.getReferencedSchema(openAPI, additionalProperties); + Schema referencedSchemaProperty = (Schema) referencedSchema.getProperties().get("resolve_inline_map_schema_in_response_property"); + + assertEquals( + "#/components/schemas/_resolve_inline_map_schema_in_response_resolve_inline_map_schema_in_response_property", + referencedSchemaProperty.get$ref() + ); + assertNotNull(ModelUtils.getReferencedSchema(openAPI, referencedSchemaProperty)); + } + @Test public void arbitraryObjectRequestBody() { OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml"); diff --git a/modules/openapi-generator/src/test/resources/3_0/inline_model_resolver.yaml b/modules/openapi-generator/src/test/resources/3_0/inline_model_resolver.yaml index a6235c7e60e..e52d01df0b6 100644 --- a/modules/openapi-generator/src/test/resources/3_0/inline_model_resolver.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/inline_model_resolver.yaml @@ -151,6 +151,15 @@ paths: properties: resolve_inline_object_response_with_additional_properties: type: string + /resolve_inline_map_schema_in_response: + get: + operationId: resolveInlineMapSchemaInResponse + responses: + '200': + description: OK + # Swagger parser doesn't use MapSchema currently, + # so we need to set a MapSchema instance as the schema manually for testing. + # see InlineModelResolverTest#resolveInlineMapSchemaInResponse() /arbitrary_object_request_body: post: requestBody: