Skip to content
Closed
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 @@ -3432,15 +3432,15 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
break;
}
currentSchemaName = queue.remove(0);
MappedModel mm = new MappedModel(currentSchemaName, toModelName(currentSchemaName));
descendentSchemas.add(mm);
Schema cs = schemas.get(currentSchemaName);
Map<String, Object> vendorExtensions = cs.getExtensions();
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);
}
String mappingName =
Optional.ofNullable(vendorExtensions)
.map(ve -> ve.get("x-discriminator-value"))
.map(discriminatorValue -> (String) discriminatorValue)
.orElse(currentSchemaName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is changing - since this value previously would have been null

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, prior to this change currentSchemaName was added as a mapping name on line 3365, and then another mapping was added on line 3371 if an x-discriminator-value was set. With this change, it's one or the other, not both.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this change is causing the issues you're seeing with Dart. I'd have to look more closely at how the schemas are being used later on, but this adds less schemas now; so perhaps some filterins logic is applied at some point?

I would try reverting this part of the change and see what the Dart output looks like.

MappedModel mm = new MappedModel(mappingName, toModelName(currentSchemaName));
descendentSchemas.add(mm);
}
return descendentSchemas;
}
Expand Down Expand Up @@ -3490,10 +3490,11 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
// for schemas that allOf inherit from this schema, add those descendants to this discriminator map
List<MappedModel> otherDescendants = getAllOfDescendants(schemaName, openAPI);
for (MappedModel otherDescendant : otherDescendants) {
// add only if the mapping names are not the same
// add only if the mapping names are not the same and the model names are not the same
boolean matched = false;
for (MappedModel uniqueDescendant : uniqueDescendants) {
if (uniqueDescendant.getMappingName().equals(otherDescendant.getMappingName())) {
if (uniqueDescendant.getMappingName().equals(otherDescendant.getMappingName())
|| (uniqueDescendant.getModelName().equals(otherDescendant.getModelName()))) {
matched = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,6 @@ public void testComposedSchemaAllOfDiscriminatorMap() {
cm = codegen.fromModel(modelName, sc);
hs.clear();
hs.add(new CodegenDiscriminator.MappedModel("b", codegen.toModelName("B")));
hs.add(new CodegenDiscriminator.MappedModel("B", codegen.toModelName("B")));
hs.add(new CodegenDiscriminator.MappedModel("C", codegen.toModelName("C")));
Assert.assertEquals(cm.getHasDiscriminatorWithNonEmptyMapping(), true);
Assert.assertEquals(cm.discriminator.getMappedModels(), hs);
Expand Down Expand Up @@ -1583,8 +1582,6 @@ public void verifyXDiscriminatorValue() {
discriminator.setPropertyBaseName(prop);
discriminator.setMapping(null);
discriminator.setMappedModels(new HashSet<CodegenDiscriminator.MappedModel>() {{
add(new CodegenDiscriminator.MappedModel("DailySubObj", "DailySubObj"));
add(new CodegenDiscriminator.MappedModel("SubObj", "SubObj"));
add(new CodegenDiscriminator.MappedModel("daily", "DailySubObj"));
add(new CodegenDiscriminator.MappedModel("sub-obj", "SubObj"));
}});
Expand Down Expand Up @@ -1991,8 +1988,6 @@ private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
test.getMapping().put("c", "Child");
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("a", "Adult"));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("c", "Child"));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("Adult", "Adult"));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("Child", "Child"));
Assert.assertEquals(discriminator, test);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ public void testDiscriminatorWithMappingIssue14731() throws IOException {
codegen.setHateoas(true);
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");


codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);
Expand Down Expand Up @@ -1271,6 +1271,44 @@ public void testDiscriminatorWithoutMappingIssue14731() throws IOException {
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/ChildWithoutMappingBDTO.java"), "@JsonTypeName");
}

@Test
public void testMappingSubtypesIssue13150() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_13150.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.setUseOneOfInterfaces(true);

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
codegen.setHateoas(true);
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");

codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);

generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();

String jsonSubType = "@JsonSubTypes({\n" +
" @JsonSubTypes.Type(value = Foo.class, name = \"foo\")\n" +
"})";
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Parent.java"), jsonSubType);
}

@Test
public void testTypeMappings() {
final SpringCodegen codegen = new SpringCodegen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,6 @@ public void allOfTest() {
Set<CodegenDiscriminator.MappedModel> mappedModels = new LinkedHashSet<CodegenDiscriminator.MappedModel>();
mappedModels.add(new CodegenDiscriminator.MappedModel("a", "Adult"));
mappedModels.add(new CodegenDiscriminator.MappedModel("c", "Child"));
mappedModels.add(new CodegenDiscriminator.MappedModel("Adult", "Adult"));
mappedModels.add(new CodegenDiscriminator.MappedModel("Child", "Child"));
Assert.assertEquals(codegenDiscriminator.getMappedModels(), mappedModels);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,11 +1483,14 @@ components:
Animal:
type: object
discriminator:
propertyName: className
propertyName: species
mapping:
DOG: '#/components/schemas/Dog'
CAT: '#/components/schemas/Cat'
required:
- className
- species
properties:
className:
species:
type: string
color:
type: string
Expand Down
60 changes: 60 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_13150.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'FooService'
paths:
/parent:
put:
summary: put parent
operationId: putParent
parameters:
- name: name
in: path
required: true
description: Name of the account being updated.
schema:
type: string
requestBody:
description: The updated account definition to save.
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Parent'
responses:
'200':
$ref: '#/components/responses/Parent'
components:
schemas:
Parent:
type: object
description: Defines an account by name.
properties:
name:
type: string
description: The account name.
type:
type: string
description: The account type discriminator.
required:
- name
- type
discriminator:
propertyName: type
mapping:
foo: '#/components/schemas/Foo'

Foo:
allOf:
- $ref: "#/components/schemas/Parent"
- type: object
properties:
fooType:
type: string
responses:
Parent:
description: The saved account definition.
content:
application/json:
schema:
$ref: '#/components/schemas/Parent'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ClassName** | **string** | |
**Species** | **string** | |
**Color** | **string** | | [optional] [default to "red"]

[[Back to Model list]](../README.md#documentation-for-models)
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/csharp/OpenAPIClient/docs/Cat.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ClassName** | **string** | |
**Species** | **string** | |
**Color** | **string** | | [optional] [default to "red"]
**Declawed** | **bool** | | [optional]

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/csharp/OpenAPIClient/docs/Dog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ClassName** | **string** | |
**Species** | **string** | |
**Color** | **string** | | [optional] [default to "red"]
**Breed** | **string** | | [optional]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract]
[JsonConverter(typeof(JsonSubtypes), "className")]
[JsonConverter(typeof(JsonSubtypes), "species")]
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand All @@ -42,18 +42,18 @@ protected Animal() { }
/// <summary>
/// Initializes a new instance of the <see cref="Animal" /> class.
/// </summary>
/// <param name="className">className (required).</param>
/// <param name="species">species (required).</param>
/// <param name="color">color (default to &quot;red&quot;).</param>
public Animal(string className = default(string), string color = "red")
public Animal(string species = default(string), string color = "red")
{
// to ensure "className" is required (not null)
if (className == null)
// to ensure "species" is required (not null)
if (species == null)
{
throw new InvalidDataException("className is a required property for Animal and cannot be null");
throw new InvalidDataException("species is a required property for Animal and cannot be null");
}
else
{
this.ClassName = className;
this.Species = species;
}

// use default value if no "color" provided
Expand All @@ -68,10 +68,10 @@ protected Animal() { }
}

/// <summary>
/// Gets or Sets ClassName
/// Gets or Sets Species
/// </summary>
[DataMember(Name="className", EmitDefaultValue=true)]
public string ClassName { get; set; }
[DataMember(Name="species", EmitDefaultValue=true)]
public string Species { get; set; }

/// <summary>
/// Gets or Sets Color
Expand All @@ -87,7 +87,7 @@ public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class Animal {\n");
sb.Append(" ClassName: ").Append(ClassName).Append("\n");
sb.Append(" Species: ").Append(Species).Append("\n");
sb.Append(" Color: ").Append(Color).Append("\n");
sb.Append("}\n");
return sb.ToString();
Expand Down Expand Up @@ -124,9 +124,9 @@ public bool Equals(Animal input)

return
(
this.ClassName == input.ClassName ||
(this.ClassName != null &&
this.ClassName.Equals(input.ClassName))
this.Species == input.Species ||
(this.Species != null &&
this.Species.Equals(input.Species))
) &&
(
this.Color == input.Color ||
Expand All @@ -144,8 +144,8 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.ClassName != null)
hashCode = hashCode * 59 + this.ClassName.GetHashCode();
if (this.Species != null)
hashCode = hashCode * 59 + this.Species.GetHashCode();
if (this.Color != null)
hashCode = hashCode * 59 + this.Color.GetHashCode();
return hashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected Cat() { }
/// Initializes a new instance of the <see cref="Cat" /> class.
/// </summary>
/// <param name="declawed">declawed.</param>
public Cat(bool declawed = default(bool), string className = "Cat", string color = "red") : base(className, color)
public Cat(bool declawed = default(bool), string species = "Cat", string color = "red") : base(species, color)
{
this.Declawed = declawed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected Dog() { }
/// Initializes a new instance of the <see cref="Dog" /> class.
/// </summary>
/// <param name="breed">breed.</param>
public Dog(string breed = default(string), string className = "Dog", string color = "red") : base(className, color)
public Dog(string breed = default(string), string species = "Dog", string color = "red") : base(species, color)
{
this.Breed = breed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ defmodule OpenapiPetstore.Model.Animal do

@derive [Poison.Encoder]
defstruct [
:className,
:species,
:color
]

@type t :: %__MODULE__{
:className => String.t,
:species => String.t,
:color => String.t | nil
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ defmodule OpenapiPetstore.Model.Cat do

@derive [Poison.Encoder]
defstruct [
:className,
:species,
:color,
:declawed
]

@type t :: %__MODULE__{
:className => String.t,
:species => String.t,
:color => String.t | nil,
:declawed => boolean() | nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ defmodule OpenapiPetstore.Model.Dog do

@derive [Poison.Encoder]
defstruct [
:className,
:species,
:color,
:breed
]

@type t :: %__MODULE__{
:className => String.t,
:species => String.t,
:color => String.t | nil,
:breed => String.t | nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**className** | **String** | | |
|**species** | **String** | | |
|**color** | **String** | | [optional] |


Expand Down
Loading