[Java] Handle discriminator mapping non-ref name (#3247)

The `mapping` property of the [Discriminator Object] is "An object to
hold mappings between payload values and schema names or references."
The subsequent examples in the spec have `mapping`s of both types.
The `mapping` support introduced in #536 only supports references.

Update the code to support names (identified by lack of `/`) or
references and change a test mapping to cover this case.

[Discriminator Object]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#discriminatorObject

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
This commit is contained in:
Kevin Locke 2019-07-04 07:24:47 +00:00 committed by William Cheng
parent 310f5feda0
commit 2c342cc2b4
3 changed files with 6 additions and 4 deletions

View File

@ -1867,8 +1867,10 @@ public class DefaultCodegen implements CodegenConfig {
discriminator.setMapping(schema.getDiscriminator().getMapping());
if (schema.getDiscriminator().getMapping() != null && !schema.getDiscriminator().getMapping().isEmpty()) {
for (Entry<String, String> e : schema.getDiscriminator().getMapping().entrySet()) {
String name = toModelName(ModelUtils.getSimpleRef(e.getValue())); // e.g e.getValue => #/components/schemas/Dog
discriminator.getMappedModels().add(new MappedModel(e.getKey(), name));
String nameOrRef = e.getValue();
String name = nameOrRef.indexOf('/') >= 0 ? ModelUtils.getSimpleRef(nameOrRef) : nameOrRef;
String modelName = toModelName(name);
discriminator.getMappedModels().add(new MappedModel(e.getKey(), modelName));
}
} else {
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);

View File

@ -583,7 +583,7 @@ public class DefaultCodegenTest {
test.setPropertyName("DollarUnderscoretype");
test.setMapping(new HashMap<>());
test.getMapping().put("a", "#/components/schemas/Adult");
test.getMapping().put("c", "#/components/schemas/Child");
test.getMapping().put("c", "Child");
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("a", "Adult"));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("c", "Child"));
Assert.assertEquals(discriminator, test);

View File

@ -32,7 +32,7 @@ components:
propertyName: $_type
mapping:
a: '#/components/schemas/Adult'
c: '#/components/schemas/Child'
c: Child
properties:
$_type:
type: string