diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java index e5a041c2010..48ade3ebc5c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java @@ -377,7 +377,7 @@ public class ExampleGenerator { return null; } return resolvePropertyToExample(name, mediaType, found.get(), processedModels); - } else if (ModelUtils.isArraySchema(schema)) { + } else if (ModelUtils.isArraySchema(schema) || ModelUtils.isEnumSchema(schema)) { return resolvePropertyToExample(schema.getName(), mediaType, schema, processedModels); } else { // TODO log an error message as the model does not have any properties diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 4c663e5e0a4..0aec52f4c1c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -20,7 +20,6 @@ package org.openapitools.codegen.utils; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.core.util.AnnotationsUtils; -import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.PathItem; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/yaml/YamlGeneratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/yaml/YamlGeneratorTest.java index 6f289d538e1..6c26f20f50f 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/yaml/YamlGeneratorTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/yaml/YamlGeneratorTest.java @@ -28,6 +28,7 @@ import org.testng.annotations.Test; import java.io.File; import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -121,4 +122,31 @@ public class YamlGeneratorTest { Assert.assertEquals(actual.getPaths().get("/foo/bar").getPost().getResponses().get("200").getContent().get("*/*").getSchema().getAdditionalProperties(), expected.getComponents().getSchemas().get("_foo_bar_post_200_response").getAdditionalProperties()); } + + @Test + public void testIssue18622() throws Exception { + Map properties = new HashMap<>(); + properties.put(OpenAPIYamlGenerator.OUTPUT_NAME, "issue_18622.yaml"); + + File output = Files.createTempDirectory("issue_18622").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("openapi-yaml") + .setAdditionalProperties(properties) + .setInputSpec("src/test/resources/2_0/issue_18622.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(clientOptInput).generate(); + Assert.assertEquals(files.size(), 5); + TestUtils.ensureContainsFile(files, output, "issue_18622.yaml"); + + OpenAPI expected = TestUtils.parseSpec("src/test/resources/2_0/issue_18622_expected.yaml"); + OpenAPI actual = TestUtils.parseSpec(Path.of(output.getAbsolutePath(), "issue_18622.yaml").toString()); + + Assert.assertEquals(actual.getComponents().getSchemas().get("myresponse").getExample(), + expected.getComponents().getSchemas().get("myresponse").getExample()); + } } diff --git a/modules/openapi-generator/src/test/resources/2_0/issue_18622.yaml b/modules/openapi-generator/src/test/resources/2_0/issue_18622.yaml new file mode 100644 index 00000000000..9a3ea0f7eb7 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/2_0/issue_18622.yaml @@ -0,0 +1,27 @@ +swagger: '2.0' +info: + title: 'Buggy Api' + version: '1.0' +consumes: + - application/json +paths: + /foo/bar: + post: + responses: + '200': + description: ok + schema: + $ref: "#/definitions/myresponse" +definitions: + myresponse: + type: object + additionalProperties: false + properties: + x: + $ref: "#/definitions/myenum" + myenum: + type: string + enum: + - A + - B + - C diff --git a/modules/openapi-generator/src/test/resources/2_0/issue_18622_expected.yaml b/modules/openapi-generator/src/test/resources/2_0/issue_18622_expected.yaml new file mode 100644 index 00000000000..a81bd8e86f8 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/2_0/issue_18622_expected.yaml @@ -0,0 +1,33 @@ +openapi: 3.0.1 +info: + title: Buggy Api + version: "1.0" +servers: + - url: / +paths: + /foo/bar: + post: + responses: + "200": + content: + '*/*': + schema: + $ref: '#/components/schemas/myresponse' + description: ok +components: + schemas: + myresponse: + additionalProperties: false + example: + x: A + properties: + x: + $ref: '#/components/schemas/myenum' + type: object + myenum: + enum: + - A + - B + - C + type: string +x-original-swagger-version: "2.0" diff --git a/samples/client/petstore/csharp/generichost/net8/FormModels/api/openapi.yaml b/samples/client/petstore/csharp/generichost/net8/FormModels/api/openapi.yaml index 1d1ef5d670a..5b9caf47755 100644 --- a/samples/client/petstore/csharp/generichost/net8/FormModels/api/openapi.yaml +++ b/samples/client/petstore/csharp/generichost/net8/FormModels/api/openapi.yaml @@ -1276,7 +1276,7 @@ components: id: 0 shipDate: 2020-02-02T20:20:20.000222Z complete: false - status: null + status: placed properties: id: format: int64 @@ -1400,7 +1400,7 @@ components: id: 1 - name: name id: 1 - status: null + status: available properties: id: format: int64