Skip to content

Commit

Permalink
[Java] Handle discriminator mapping non-ref name (OpenAPITools#3247)
Browse files Browse the repository at this point in the history
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 OpenAPITools#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 <[email protected]>
  • Loading branch information
kevinoid authored and wing328 committed Jul 4, 2019
1 parent 310f5fe commit 2c342cc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1867,8 +1867,10 @@ private CodegenDiscriminator createDiscriminator(String schemaName, Schema schem
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ components:
propertyName: $_type
mapping:
a: '#/components/schemas/Adult'
c: '#/components/schemas/Child'
c: Child
properties:
$_type:
type: string
Expand Down

0 comments on commit 2c342cc

Please sign in to comment.