Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ public String getDiscriminatorName() {
return discriminator == null ? null : discriminator.getPropertyName();
}

public String getDiscriminatorBaseName() {
return discriminator == null ? null : discriminator.getPropertyBaseName();
}

public ExternalDocumentation getExternalDocumentation() {
return externalDocumentation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ private void postProcessEnumRefs(final Map<String, ModelsMap> models) {
for (String openAPIName : models.keySet()) {
CodegenModel model = ModelUtils.getModelByName(openAPIName, models);
if (model != null) {
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
final Schema parentModel = allDefinitions.get(toModelName(model.parent));
final CodegenModel parentCodegenModel = parentModel != null ? super.fromModel(model.parent, parentModel) : null;

for (CodegenProperty var : model.allVars) {
if (enumRefs.containsKey(var.dataType)) {
// Handle any enum properties referred to by $ref.
Expand All @@ -611,6 +615,8 @@ private void postProcessEnumRefs(final Map<String, ModelsMap> models) {

// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
var.isPrimitiveType = true;

updateCodegenPropertyEnumDefaultValue(var, parentCodegenModel);
}
}
for (CodegenProperty var : model.vars) {
Expand All @@ -624,6 +630,8 @@ private void postProcessEnumRefs(final Map<String, ModelsMap> models) {

// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
var.isPrimitiveType = true;

updateCodegenPropertyEnumDefaultValue(var, parentCodegenModel);
}
}
for (CodegenProperty var : model.readWriteVars) {
Expand All @@ -637,6 +645,8 @@ private void postProcessEnumRefs(final Map<String, ModelsMap> models) {

// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
var.isPrimitiveType = true;

updateCodegenPropertyEnumDefaultValue(var, parentCodegenModel);
}
}
for (CodegenProperty var : model.readOnlyVars) {
Expand All @@ -650,6 +660,8 @@ private void postProcessEnumRefs(final Map<String, ModelsMap> models) {

// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
var.isPrimitiveType = true;

updateCodegenPropertyEnumDefaultValue(var, parentCodegenModel);
}
}

Expand Down Expand Up @@ -709,6 +721,30 @@ private void postProcessEnumRefs(final Map<String, ModelsMap> models) {
}
}

private void updateCodegenPropertyEnumDefaultValue(CodegenProperty var, CodegenModel parentCodegenModel) {
if (var.defaultValue != null && parentCodegenModel != null) {
String rawDefaultValue = var.defaultValue.replace("\"", "");

if (parentCodegenModel.discriminator != null) {
for (Map.Entry<String, String> mapping : parentCodegenModel.discriminator.getMapping().entrySet()) {
String candidate;
if (mapping.getValue().indexOf('/') >= 0) {
candidate = ModelUtils.getSimpleRef(mapping.getValue());
if (ModelUtils.getSchema(openAPI, candidate) == null) {
LOGGER.error("Failed to lookup the schema '{}' when processing the discriminator mapping of oneOf/anyOf. Please check to ensure it's defined properly.", candidate);
}
} else {
candidate = mapping.getValue();
}

if (rawDefaultValue.equals(candidate)) {
var.defaultValue = var.datatypeWithEnum + "." + mapping.getKey();
}
}
}
}
}

/**
* Update codegen property's enum by adding "enumVars" (with name and value)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{{/vendorExtensions.x-com-visible}}
[DataContract(Name = "{{{name}}}")]
{{#discriminator}}
[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")]
[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorBaseName}}}")] // {{{discriminatorName}}}
{{#mappedModels}}
[JsonSubtypes.KnownSubType(typeof({{{modelName}}}), "{{^vendorExtensions.x-discriminator-value}}{{{mappingName}}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{.}}}{{/vendorExtensions.x-discriminator-value}}")]
{{/mappedModels}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void simpleModelTest() {
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.vars.size(), 7);
Assert.assertEquals(cm.getDiscriminatorName(),"test");
Assert.assertEquals(cm.getDiscriminatorBaseName(),"test");

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract(Name = "Animal")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Cat
/// </summary>
[DataContract(Name = "Cat")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Cat : Animal, IEquatable<Cat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ChildCat
/// </summary>
[DataContract(Name = "ChildCat")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
public partial class ChildCat : ParentPet, IEquatable<ChildCat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Dog
/// </summary>
[DataContract(Name = "Dog")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Dog : Animal, IEquatable<Dog>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// GrandparentAnimal
/// </summary>
[DataContract(Name = "GrandparentAnimal")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
[JsonSubtypes.KnownSubType(typeof(ParentPet), "ParentPet")]
public partial class GrandparentAnimal : IEquatable<GrandparentAnimal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ParentPet
/// </summary>
[DataContract(Name = "ParentPet")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
public partial class ParentPet : GrandparentAnimal, IEquatable<ParentPet>, IValidatableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>Org.OpenAPITools.Test</AssemblyName>
<RootNamespace>Org.OpenAPITools.Test</RootNamespace>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract(Name = "Animal")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Org.OpenAPITools.Model
/// Cat
/// </summary>
[DataContract(Name = "Cat")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Cat : Animal, IEquatable<Cat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Org.OpenAPITools.Model
/// ChildCat
/// </summary>
[DataContract(Name = "ChildCat")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
public partial class ChildCat : ParentPet, IEquatable<ChildCat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Org.OpenAPITools.Model
/// Dog
/// </summary>
[DataContract(Name = "Dog")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Dog : Animal, IEquatable<Dog>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Org.OpenAPITools.Model
/// GrandparentAnimal
/// </summary>
[DataContract(Name = "GrandparentAnimal")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
[JsonSubtypes.KnownSubType(typeof(ParentPet), "ParentPet")]
public partial class GrandparentAnimal : IEquatable<GrandparentAnimal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Org.OpenAPITools.Model
/// ParentPet
/// </summary>
[DataContract(Name = "ParentPet")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
public partial class ParentPet : GrandparentAnimal, IEquatable<ParentPet>, IValidatableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract(Name = "Animal")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Cat
/// </summary>
[DataContract(Name = "Cat")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Cat : Animal, IEquatable<Cat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ChildCat
/// </summary>
[DataContract(Name = "ChildCat")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
public partial class ChildCat : ParentPet, IEquatable<ChildCat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Dog
/// </summary>
[DataContract(Name = "Dog")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Dog : Animal, IEquatable<Dog>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// GrandparentAnimal
/// </summary>
[DataContract(Name = "GrandparentAnimal")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
[JsonSubtypes.KnownSubType(typeof(ParentPet), "ParentPet")]
public partial class GrandparentAnimal : IEquatable<GrandparentAnimal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ParentPet
/// </summary>
[DataContract(Name = "ParentPet")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
public partial class ParentPet : GrandparentAnimal, IEquatable<ParentPet>, IValidatableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract(Name = "Animal")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Cat
/// </summary>
[DataContract(Name = "Cat")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Cat : Animal, IEquatable<Cat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ChildCat
/// </summary>
[DataContract(Name = "ChildCat")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
public partial class ChildCat : ParentPet, IEquatable<ChildCat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Dog
/// </summary>
[DataContract(Name = "Dog")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Dog : Animal, IEquatable<Dog>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// GrandparentAnimal
/// </summary>
[DataContract(Name = "GrandparentAnimal")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
[JsonSubtypes.KnownSubType(typeof(ParentPet), "ParentPet")]
public partial class GrandparentAnimal : IEquatable<GrandparentAnimal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ParentPet
/// </summary>
[DataContract(Name = "ParentPet")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
public partial class ParentPet : GrandparentAnimal, IEquatable<ParentPet>, IValidatableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract(Name = "Animal")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Cat
/// </summary>
[DataContract(Name = "Cat")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Cat : Animal, IEquatable<Cat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ChildCat
/// </summary>
[DataContract(Name = "ChildCat")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
public partial class ChildCat : ParentPet, IEquatable<ChildCat>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Dog
/// </summary>
[DataContract(Name = "Dog")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
public partial class Dog : Animal, IEquatable<Dog>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// GrandparentAnimal
/// </summary>
[DataContract(Name = "GrandparentAnimal")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
[JsonSubtypes.KnownSubType(typeof(ParentPet), "ParentPet")]
public partial class GrandparentAnimal : IEquatable<GrandparentAnimal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// ParentPet
/// </summary>
[DataContract(Name = "ParentPet")]
[JsonConverter(typeof(JsonSubtypes), "PetType")]
[JsonConverter(typeof(JsonSubtypes), "pet_type")] // PetType
[JsonSubtypes.KnownSubType(typeof(ChildCat), "ChildCat")]
public partial class ParentPet : GrandparentAnimal, IEquatable<ParentPet>, IValidatableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract(Name = "Animal")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonConverter(typeof(JsonSubtypes), "className")] // ClassName
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand Down
Loading