Skip to content

Commit

Permalink
fix: Null example values generated for enum properties (#18623)
Browse files Browse the repository at this point in the history
* Fix null example values being generated for enum properties

* Update examples

* PRFB: use isEnumSchema
  • Loading branch information
alexrjones committed May 14, 2024
1 parent edbb021 commit 2f73451
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private Object resolveModelToExample(String name, String mediaType, Schema schem
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

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;
Expand Down Expand Up @@ -121,4 +122,31 @@ public void testIssue9086() throws Exception {
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<String, Object> 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<File> 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());
}
}
27 changes: 27 additions & 0 deletions modules/openapi-generator/src/test/resources/2_0/issue_18622.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ components:
id: 0
shipDate: 2020-02-02T20:20:20.000222Z
complete: false
status: null
status: placed
properties:
id:
format: int64
Expand Down Expand Up @@ -1400,7 +1400,7 @@ components:
id: 1
- name: name
id: 1
status: null
status: available
properties:
id:
format: int64
Expand Down

0 comments on commit 2f73451

Please sign in to comment.