Skip to content

Commit

Permalink
[JAVA] Prioritize mapped discriminators over generated (relates to is…
Browse files Browse the repository at this point in the history
…sue #12777) (#15284)

* prioritize mapped discriminators over generated

* update samples with new ordering

* explain reason behind discriminator prioritization

* add new samples

* prioritize explicit mapping over any generated mappings

* update examples to reflect new logic

* update tests to reflect explicit mappings
  • Loading branch information
robbertvanwaveren authored Sep 27, 2023
1 parent 4f8d61c commit beb67aa
Show file tree
Hide file tree
Showing 13 changed files with 693 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,16 @@ public static class MappedModel implements Comparable<MappedModel>{

private CodegenModel model;

public MappedModel(String mappingName, String modelName) {
private final boolean explicitMapping;

public MappedModel(String mappingName, String modelName, boolean explicitMapping) {
this.mappingName = mappingName;
this.modelName = modelName;
this.explicitMapping = explicitMapping;
}

public MappedModel(String mappingName, String modelName) {
this(mappingName, modelName, false);
}

@Override
Expand All @@ -137,7 +144,14 @@ public int compareTo(MappedModel other) {
} else if (other.getMappingName() == null) {
return -1;
}
return getMappingName().compareTo(other.getMappingName());

// prioritize mappings based on mappings in the spec before any auto-generated
// so that during serialization the proper values are used in the json
if (explicitMapping != other.explicitMapping) {
return explicitMapping ? -1 : 1;
} else {
return getMappingName().compareTo(other.getMappingName());
}
}

public String getMappingName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3492,7 +3492,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
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(modelName));
mm = new MappedModel(xDiscriminatorValue, toModelName(modelName), true);
descendentSchemas.add(mm);
}
}
Expand Down Expand Up @@ -3550,7 +3550,7 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName) {
.map(ve -> ve.get("x-discriminator-value"))
.map(discriminatorValue -> (String) discriminatorValue)
.orElse(currentSchemaName);
MappedModel mm = new MappedModel(mappingName, toModelName(currentSchemaName));
MappedModel mm = new MappedModel(mappingName, toModelName(currentSchemaName), mappingName != currentSchemaName);
descendentSchemas.add(mm);
}
return descendentSchemas;
Expand Down Expand Up @@ -3604,7 +3604,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
} else {
name = e.getValue();
}
uniqueDescendants.add(new MappedModel(e.getKey(), toModelName(name)));
uniqueDescendants.add(new MappedModel(e.getKey(), toModelName(name), true));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ public void testComposedSchemaAllOfDiscriminatorMap() {
sc = openAPI.getComponents().getSchemas().get(modelName);
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"), true));
hs.add(new CodegenDiscriminator.MappedModel("C", codegen.toModelName("C")));
Assert.assertEquals(cm.getHasDiscriminatorWithNonEmptyMapping(), true);
Assert.assertEquals(cm.discriminator.getMappedModels(), hs);
Expand All @@ -1174,7 +1174,7 @@ public void testComposedSchemaAllOfDiscriminatorMap() {
sc = openAPI.getComponents().getSchemas().get(modelName);
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"), true));
hs.add(new CodegenDiscriminator.MappedModel("C", codegen.toModelName("C")));
Assert.assertEquals(cm.getHasDiscriminatorWithNonEmptyMapping(), true);
Assert.assertEquals(cm.discriminator.getMappedModels(), hs);
Expand All @@ -1184,7 +1184,7 @@ public void testComposedSchemaAllOfDiscriminatorMap() {
sc = openAPI.getComponents().getSchemas().get(modelName);
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"), true));
Assert.assertEquals(cm.getHasDiscriminatorWithNonEmptyMapping(), true);
Assert.assertEquals(cm.discriminator.getMappedModels(), hs);
}
Expand Down Expand Up @@ -1265,7 +1265,7 @@ public void testComposedSchemaAllOfDiscriminatorMapLegacy() {
sc = openAPI.getComponents().getSchemas().get(modelName);
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"), true));
Assert.assertEquals(cm.discriminator.getMappedModels(), hs);

// the mapping in b is in B
Expand Down Expand Up @@ -1654,8 +1654,8 @@ public void verifyXDiscriminatorValue() {
discriminator.setPropertyBaseName(prop);
discriminator.setMapping(null);
discriminator.setMappedModels(new HashSet<CodegenDiscriminator.MappedModel>() {{
add(new CodegenDiscriminator.MappedModel("daily", "DailySubObj"));
add(new CodegenDiscriminator.MappedModel("sub-obj", "SubObj"));
add(new CodegenDiscriminator.MappedModel("daily", "DailySubObj", true));
add(new CodegenDiscriminator.MappedModel("sub-obj", "SubObj", true));
}});
assertEquals(cm.discriminator, discriminator);
}
Expand Down Expand Up @@ -2049,8 +2049,8 @@ private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
test.setMapping(new HashMap<>());
test.getMapping().put("a", "#/components/schemas/Adult");
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("a", "Adult", true));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("c", "Child", true));
Assert.assertEquals(discriminator, test);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ public void allOfTest() {

CodegenDiscriminator codegenDiscriminator = person.getDiscriminator();
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("a", "Adult", true));
mappedModels.add(new CodegenDiscriminator.MappedModel("c", "Child", true));
Assert.assertEquals(codegenDiscriminator.getMappedModels(), mappedModels);
}

Expand All @@ -405,8 +405,8 @@ public void allOfTestLegacy() {

CodegenDiscriminator codegenDiscriminator = person.getDiscriminator();
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("a", "Adult", true));
mappedModels.add(new CodegenDiscriminator.MappedModel("c", "Child", true));
Assert.assertEquals(codegenDiscriminator.getMappedModels(), mappedModels);
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
Defaulting to user installation because normal site-packages is not writeable
Collecting tox
Downloading tox-4.4.12-py3-none-any.whl (148 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 149.0/149.0 KB 3.0 MB/s eta 0:00:00
Collecting flake8
Downloading flake8-6.0.0-py2.py3-none-any.whl (57 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.8/57.8 KB 10.7 MB/s eta 0:00:00
Collecting chardet>=5.1
Downloading chardet-5.1.0-py3-none-any.whl (199 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 199.1/199.1 KB 8.7 MB/s eta 0:00:00
Collecting tomli>=2.0.1
Downloading tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting filelock>=3.11
Downloading filelock-3.12.0-py3-none-any.whl (10 kB)
Collecting platformdirs>=3.2
Downloading platformdirs-3.2.0-py3-none-any.whl (14 kB)
Collecting pyproject-api>=1.5.1
Downloading pyproject_api-1.5.1-py3-none-any.whl (12 kB)
Collecting packaging>=23
Downloading packaging-23.1-py3-none-any.whl (48 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 48.9/48.9 KB 6.8 MB/s eta 0:00:00
Collecting pluggy>=1
Downloading pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
Collecting virtualenv>=20.21
Downloading virtualenv-20.22.0-py3-none-any.whl (3.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2/3.2 MB 14.5 MB/s eta 0:00:00
Collecting colorama>=0.4.6
Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting cachetools>=5.3
Downloading cachetools-5.3.0-py3-none-any.whl (9.3 kB)
Collecting mccabe<0.8.0,>=0.7.0
Downloading mccabe-0.7.0-py2.py3-none-any.whl (7.3 kB)
Collecting pycodestyle<2.11.0,>=2.10.0
Downloading pycodestyle-2.10.0-py2.py3-none-any.whl (41 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.3/41.3 KB 5.0 MB/s eta 0:00:00
Collecting pyflakes<3.1.0,>=3.0.0
Downloading pyflakes-3.0.1-py2.py3-none-any.whl (62 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 KB 14.3 MB/s eta 0:00:00
Requirement already satisfied: distlib<1,>=0.3.6 in /home/robbert/.local/lib/python3.10/site-packages (from virtualenv>=20.21->tox->-r dev-requirements.txt (line 1)) (0.3.6)
Installing collected packages: tomli, pyflakes, pycodestyle, pluggy, platformdirs, packaging, mccabe, filelock, colorama, chardet, cachetools, virtualenv, pyproject-api, flake8, tox
Attempting uninstall: filelock
Found existing installation: filelock 3.10.4
Uninstalling filelock-3.10.4:
Successfully uninstalled filelock-3.10.4
Attempting uninstall: virtualenv
Found existing installation: virtualenv 20.4.2
Uninstalling virtualenv-20.4.2:
Successfully uninstalled virtualenv-20.4.2
Successfully installed cachetools-5.3.0 chardet-5.1.0 colorama-0.4.6 filelock-3.12.0 flake8-6.0.0 mccabe-0.7.0 packaging-23.1 platformdirs-3.2.0 pluggy-1.0.0 pycodestyle-2.10.0 pyflakes-3.0.1 pyproject-api-1.5.1 tomli-2.0.1 tox-4.4.12 virtualenv-20.22.0
Collecting tox (from -r dev-requirements.txt (line 1))
Using cached tox-4.4.12-py3-none-any.whl (148 kB)
Collecting flake8 (from -r dev-requirements.txt (line 2))
Using cached flake8-6.0.0-py2.py3-none-any.whl (57 kB)
Collecting cachetools>=5.3 (from tox->-r dev-requirements.txt (line 1))
Using cached cachetools-5.3.0-py3-none-any.whl (9.3 kB)
Collecting chardet>=5.1 (from tox->-r dev-requirements.txt (line 1))
Using cached chardet-5.1.0-py3-none-any.whl (199 kB)
Collecting colorama>=0.4.6 (from tox->-r dev-requirements.txt (line 1))
Using cached colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting filelock>=3.11 (from tox->-r dev-requirements.txt (line 1))
Using cached filelock-3.12.0-py3-none-any.whl (10 kB)
Collecting packaging>=23 (from tox->-r dev-requirements.txt (line 1))
Using cached packaging-23.1-py3-none-any.whl (48 kB)
Collecting platformdirs>=3.2 (from tox->-r dev-requirements.txt (line 1))
Using cached platformdirs-3.2.0-py3-none-any.whl (14 kB)
Collecting pluggy>=1 (from tox->-r dev-requirements.txt (line 1))
Using cached pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
Collecting pyproject-api>=1.5.1 (from tox->-r dev-requirements.txt (line 1))
Using cached pyproject_api-1.5.1-py3-none-any.whl (12 kB)
Collecting tomli>=2.0.1 (from tox->-r dev-requirements.txt (line 1))
Using cached tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting virtualenv>=20.21 (from tox->-r dev-requirements.txt (line 1))
Using cached virtualenv-20.22.0-py3-none-any.whl (3.2 MB)
Collecting mccabe<0.8.0,>=0.7.0 (from flake8->-r dev-requirements.txt (line 2))
Using cached mccabe-0.7.0-py2.py3-none-any.whl (7.3 kB)
Collecting pycodestyle<2.11.0,>=2.10.0 (from flake8->-r dev-requirements.txt (line 2))
Using cached pycodestyle-2.10.0-py2.py3-none-any.whl (41 kB)
Collecting pyflakes<3.1.0,>=3.0.0 (from flake8->-r dev-requirements.txt (line 2))
Using cached pyflakes-3.0.1-py2.py3-none-any.whl (62 kB)
Collecting distlib<1,>=0.3.6 (from virtualenv>=20.21->tox->-r dev-requirements.txt (line 1))
Using cached distlib-0.3.6-py2.py3-none-any.whl (468 kB)
Installing collected packages: distlib, tomli, pyflakes, pycodestyle, pluggy, platformdirs, packaging, mccabe, filelock, colorama, chardet, cachetools, virtualenv, pyproject-api, flake8, tox
Successfully installed cachetools-5.3.0 chardet-5.1.0 colorama-0.4.6 distlib-0.3.6 filelock-3.12.0 flake8-6.0.0 mccabe-0.7.0 packaging-23.1 platformdirs-3.2.0 pluggy-1.0.0 pycodestyle-2.10.0 pyflakes-3.0.1 pyproject-api-1.5.1 tomli-2.0.1 tox-4.4.12 virtualenv-20.22.0
Requirement already satisfied: tox in ./venv/lib/python3.10/site-packages (from -r dev-requirements.txt (line 1)) (4.4.12)
Requirement already satisfied: flake8 in ./venv/lib/python3.10/site-packages (from -r dev-requirements.txt (line 2)) (6.0.0)
Requirement already satisfied: cachetools>=5.3 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (5.3.0)
Requirement already satisfied: chardet>=5.1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (5.1.0)
Requirement already satisfied: colorama>=0.4.6 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (0.4.6)
Requirement already satisfied: filelock>=3.11 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (3.12.0)
Requirement already satisfied: packaging>=23 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (23.1)
Requirement already satisfied: platformdirs>=3.2 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (3.2.0)
Requirement already satisfied: pluggy>=1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (1.0.0)
Requirement already satisfied: pyproject-api>=1.5.1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (1.5.1)
Requirement already satisfied: tomli>=2.0.1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (2.0.1)
Requirement already satisfied: virtualenv>=20.21 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (20.22.0)
Requirement already satisfied: mccabe<0.8.0,>=0.7.0 in ./venv/lib/python3.10/site-packages (from flake8->-r dev-requirements.txt (line 2)) (0.7.0)
Requirement already satisfied: pycodestyle<2.11.0,>=2.10.0 in ./venv/lib/python3.10/site-packages (from flake8->-r dev-requirements.txt (line 2)) (2.10.0)
Requirement already satisfied: pyflakes<3.1.0,>=3.0.0 in ./venv/lib/python3.10/site-packages (from flake8->-r dev-requirements.txt (line 2)) (3.0.1)
Requirement already satisfied: distlib<1,>=0.3.6 in ./venv/lib/python3.10/site-packages (from virtualenv>=20.21->tox->-r dev-requirements.txt (line 1)) (0.3.6)
Requirement already satisfied: tox in ./venv/lib/python3.10/site-packages (from -r dev-requirements.txt (line 1)) (4.4.12)
Requirement already satisfied: flake8 in ./venv/lib/python3.10/site-packages (from -r dev-requirements.txt (line 2)) (6.0.0)
Requirement already satisfied: cachetools>=5.3 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (5.3.0)
Requirement already satisfied: chardet>=5.1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (5.1.0)
Requirement already satisfied: colorama>=0.4.6 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (0.4.6)
Requirement already satisfied: filelock>=3.11 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (3.12.0)
Requirement already satisfied: packaging>=23 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (23.1)
Requirement already satisfied: platformdirs>=3.2 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (3.2.0)
Requirement already satisfied: pluggy>=1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (1.0.0)
Requirement already satisfied: pyproject-api>=1.5.1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (1.5.1)
Requirement already satisfied: tomli>=2.0.1 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (2.0.1)
Requirement already satisfied: virtualenv>=20.21 in ./venv/lib/python3.10/site-packages (from tox->-r dev-requirements.txt (line 1)) (20.22.0)
Requirement already satisfied: mccabe<0.8.0,>=0.7.0 in ./venv/lib/python3.10/site-packages (from flake8->-r dev-requirements.txt (line 2)) (0.7.0)
Requirement already satisfied: pycodestyle<2.11.0,>=2.10.0 in ./venv/lib/python3.10/site-packages (from flake8->-r dev-requirements.txt (line 2)) (2.10.0)
Requirement already satisfied: pyflakes<3.1.0,>=3.0.0 in ./venv/lib/python3.10/site-packages (from flake8->-r dev-requirements.txt (line 2)) (3.0.1)
Requirement already satisfied: distlib<1,>=0.3.6 in ./venv/lib/python3.10/site-packages (from virtualenv>=20.21->tox->-r dev-requirements.txt (line 1)) (0.3.6)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Metadata-Version: 2.1
Name: petstore-api
Version: 1.0.0
Summary: OpenAPI Petstore
Home-page:
Author: OpenAPI Generator community
Author-email: [email protected]
License: Apache-2.0
Keywords: OpenAPI,OpenAPI-Generator,OpenAPI Petstore
Description-Content-Type: text/markdown

This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \&quot; \ # noqa: E501

Loading

0 comments on commit beb67aa

Please sign in to comment.