mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 20:50:55 +00:00
fix: Null example values generated for enum properties (#18623)
* Fix null example values being generated for enum properties * Update examples * PRFB: use isEnumSchema
This commit is contained in:
parent
edbb021aad
commit
2f734515a1
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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<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());
|
||||
}
|
||||
}
|
||||
|
@ -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
|
@ -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"
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user