Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove @JsonTypeName from Java POJOs #14842

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{^-last}},{{/-last}}
{{/vars}}
})
{{#isClassnameSanitized}}
{{^hasDiscriminatorWithNonEmptyMapping}}
@JsonTypeName("{{name}}")
{{/hasDiscriminatorWithNonEmptyMapping}}
{{/isClassnameSanitized}}
{{/jackson}}
{{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
{{#vendorExtensions.x-class-extra-annotation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openapitools.codegen.java;

import com.google.common.collect.ImmutableMap;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.ArraySchema;
Expand All @@ -31,7 +30,6 @@
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
Expand Down Expand Up @@ -77,8 +75,6 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.openapitools.codegen.TestUtils.assertFileContains;
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -1757,81 +1753,4 @@ public void testJdkHttpClientWithAndWithoutParentExtension() throws Exception {
"public class AnotherChild {");
}

@Test
public void testDiscriminatorWithMappingIssue14731() 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_14731.yaml", null, new ParseOptions()).getOpenAPI();

JavaClientCodegen codegen = new JavaClientCodegen();
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();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");


codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);
codegen.setUseJakartaEe(true);
codegen.setModelNameSuffix("DTO");

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();

assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithMappingADTO.java"), "@JsonTypeName");
assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithMappingBDTO.java"), "@JsonTypeName");
}

@Test
public void testDiscriminatorWithoutMappingIssue14731() 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_14731.yaml", null, new ParseOptions()).getOpenAPI();

JavaClientCodegen codegen = new JavaClientCodegen();
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();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");


codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);
codegen.setUseJakartaEe(true);
codegen.setModelNameSuffix("DTO");
codegen.setLibrary(JavaClientCodegen.RESTTEMPLATE);


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();

assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithoutMappingADTO.java"), "@JsonTypeName");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithoutMappingBDTO.java"), "@JsonTypeName");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,14 @@ components:
properties:
declawed:
type: boolean
Special-Cat:
allOf:
- $ref: '#/definitions/Cat'
- type: object
properties:
kind:
type: string
enum: [lions, tigers, leopards, jaguars]
Animal:
type: object
discriminator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
DataQueryAllOf.JSON_PROPERTY_TEXT,
DataQueryAllOf.JSON_PROPERTY_DATE
})
@JsonTypeName("DataQuery_allOf")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class DataQueryAllOf {
public static final String JSON_PROPERTY_SUFFIX = "suffix";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.JSON_PROPERTY_ID,
TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.JSON_PROPERTY_NAME
})
@JsonTypeName("test_query_style_deepObject_explode_true_object_allOf_query_object_parameter")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter {
public static final String JSON_PROPERTY_SIZE = "size";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
@JsonPropertyOrder({
TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.JSON_PROPERTY_VALUES
})
@JsonTypeName("test_query_style_form_explode_true_array_string_query_object_parameter")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter {
public static final String JSON_PROPERTY_VALUES = "values";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ docs/PetApi.md
docs/ReadOnlyFirst.md
docs/Return.md
docs/SingleRefType.md
docs/SpecialCat.md
docs/SpecialCatAllOf.md
docs/SpecialModelName.md
docs/StoreApi.md
docs/Tag.md
Expand Down Expand Up @@ -123,6 +125,8 @@ src/Org.OpenAPITools/Model/Pet.cs
src/Org.OpenAPITools/Model/ReadOnlyFirst.cs
src/Org.OpenAPITools/Model/Return.cs
src/Org.OpenAPITools/Model/SingleRefType.cs
src/Org.OpenAPITools/Model/SpecialCat.cs
src/Org.OpenAPITools/Model/SpecialCatAllOf.cs
src/Org.OpenAPITools/Model/SpecialModelName.cs
src/Org.OpenAPITools/Model/Tag.cs
src/Org.OpenAPITools/Model/User.cs
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/csharp/OpenAPIClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ Class | Method | HTTP request | Description
- [Model.ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [Model.Return](docs/Return.md)
- [Model.SingleRefType](docs/SingleRefType.md)
- [Model.SpecialCat](docs/SpecialCat.md)
- [Model.SpecialCatAllOf](docs/SpecialCatAllOf.md)
- [Model.SpecialModelName](docs/SpecialModelName.md)
- [Model.Tag](docs/Tag.md)
- [Model.User](docs/User.md)
Expand Down
14 changes: 14 additions & 0 deletions samples/client/petstore/csharp/OpenAPIClient/docs/SpecialCat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# Org.OpenAPITools.Model.SpecialCat

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Declawed** | **bool** | | [optional]
**Kind** | **string** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models)
[[Back to API list]](../README.md#documentation-for-api-endpoints)
[[Back to README]](../README.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# Org.OpenAPITools.Model.SpecialCatAllOf

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Kind** | **string** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models)
[[Back to API list]](../README.md#documentation-for-api-endpoints)
[[Back to README]](../README.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* OpenAPI Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://github.com/openapitools/openapi-generator.git
*/


using NUnit.Framework;

using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Model;
using Org.OpenAPITools.Client;
using System.Reflection;
using Newtonsoft.Json;

namespace Org.OpenAPITools.Test
{
/// <summary>
/// Class for testing SpecialCatAllOf
/// </summary>
/// <remarks>
/// This file is automatically generated by OpenAPI Generator (https://openapi-generator.tech).
/// Please update the test case below to test the model.
/// </remarks>
public class SpecialCatAllOfTests
{
// TODO uncomment below to declare an instance variable for SpecialCatAllOf
//private SpecialCatAllOf instance;

/// <summary>
/// Setup before each test
/// </summary>
[SetUp]
public void Init()
{
// TODO uncomment below to create an instance of SpecialCatAllOf
//instance = new SpecialCatAllOf();
}

/// <summary>
/// Clean up after each test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of SpecialCatAllOf
/// </summary>
[Test]
public void SpecialCatAllOfInstanceTest()
{
// TODO uncomment below to test "IsInstanceOf" SpecialCatAllOf
//Assert.IsInstanceOf(typeof(SpecialCatAllOf), instance);
}


/// <summary>
/// Test the property 'Kind'
/// </summary>
[Test]
public void KindTest()
{
// TODO unit test for the property 'Kind'
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* OpenAPI Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://github.com/openapitools/openapi-generator.git
*/


using NUnit.Framework;

using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Model;
using Org.OpenAPITools.Client;
using System.Reflection;
using Newtonsoft.Json;

namespace Org.OpenAPITools.Test
{
/// <summary>
/// Class for testing SpecialCat
/// </summary>
/// <remarks>
/// This file is automatically generated by OpenAPI Generator (https://openapi-generator.tech).
/// Please update the test case below to test the model.
/// </remarks>
public class SpecialCatTests
{
// TODO uncomment below to declare an instance variable for SpecialCat
//private SpecialCat instance;

/// <summary>
/// Setup before each test
/// </summary>
[SetUp]
public void Init()
{
// TODO uncomment below to create an instance of SpecialCat
//instance = new SpecialCat();
}

/// <summary>
/// Clean up after each test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of SpecialCat
/// </summary>
[Test]
public void SpecialCatInstanceTest()
{
// TODO uncomment below to test "IsInstanceOf" SpecialCat
//Assert.IsInstanceOf(typeof(SpecialCat), instance);
}


/// <summary>
/// Test the property 'Kind'
/// </summary>
[Test]
public void KindTest()
{
// TODO unit test for the property 'Kind'
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Org.OpenAPITools.Model
[JsonConverter(typeof(JsonSubtypes), "className")]
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
[JsonSubtypes.KnownSubType(typeof(SpecialCat), "Special-Cat")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public override int GetHashCode()
/// <param name="validationContext">Validation context</param>
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
return this.BaseValidate(validationContext);
}

/// <summary>
/// To validate all properties of the instance
/// </summary>
/// <param name="validationContext">Validation context</param>
/// <returns>Validation Result</returns>
protected IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> BaseValidate(ValidationContext validationContext)
{
foreach(var x in base.BaseValidate(validationContext)) yield return x;
yield break;
Expand Down
Loading