Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds updates from Sebastien Rosset
  • Loading branch information
spacether committed Apr 23, 2020
commit 53ab46b6f11a11c39b6dbac7f87ffeb6ef333f93
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
import java.util.Objects;
import java.util.Set;

/**
* This class encapsulates the OpenAPI discriminator construct, as specified at
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#discriminatorObject.
*
* When request bodies or response payloads may be one of a number of different schemas,
* a discriminator object can be used to aid in serialization, deserialization, and validation.
* The discriminator is a specific object in a schema which is used to inform the consumer of
* the specification of an alternative schema based on the value associated with it.
*/
public class CodegenDiscriminator {
// The name of the property in the payload that will hold the discriminator value.
// This is the propertyName as specified in the OpenAPI discriminator object.
private String propertyName;
private String propertyBaseName;
private String propertyGetter;
Expand Down Expand Up @@ -63,8 +74,21 @@ public void setMappedModels(Set<MappedModel> mappedModels) {
this.mappedModels = mappedModels;
}

/**
* An object to hold discriminator mappings between payload values and schema names or
* references.
*
* In the OpenAPI document, the discriminator "mapping" attribute is optional.
* In scenarios where the value of the discriminator field does not match the schema name
* or implicit mapping is not possible, an optional mapping definition MAY be used.
* In OpenAPITools codegen, the MappedModel is the union of all the discriminator mappings,
* both explicitly defined in the OpenAPI document and inherited from oneOf/allOf/anyOf.
*/
public static class MappedModel implements Comparable<MappedModel>{
// The value of the discriminator property in the payload.
private String mappingName;
// The OAS schema name. It is obtained from the OAS document, and the string value
// is converted to a sanitized, internal representation within codegen.
private String modelName;

public MappedModel(String mappingName, String modelName) {
Expand Down Expand Up @@ -135,4 +159,4 @@ public String toString() {
sb.append('}');
return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2586,7 +2586,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
descendentSchemas.add(mm);
Schema cs = ModelUtils.getSchema(openAPI, modelName);
Map<String, Object> vendorExtensions = cs.getExtensions();
if (vendorExtensions != null && !vendorExtensions.isEmpty()) {
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
mm = new MappedModel(xDiscriminatorValue, toModelName(modelName));
descendentSchemas.add(mm);
Expand Down Expand Up @@ -2641,7 +2641,7 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
descendentSchemas.add(mm);
Schema cs = schemas.get(currentSchemaName);
Map<String, Object> vendorExtensions = cs.getExtensions();
if (vendorExtensions != null && !vendorExtensions.isEmpty()) {
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
mm = new MappedModel(xDiscriminatorValue, toModelName(currentSchemaName));
descendentSchemas.add(mm);
Expand Down