Skip to content

Add support x-implements for enums #2041

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

Merged
merged 3 commits into from
Mar 14, 2025
Merged
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 @@ -25,7 +25,7 @@
{{#additionalEnumTypeAnnotations}}
{{{.}}}
{{/additionalEnumTypeAnnotations}}
{{#formatSingleLine}}public enum {{>common/model/enumName}}{{/formatSingleLine}} {
{{#formatSingleLine}}public enum {{>common/model/enumName}}{{#vendorExtensions.x-implements}}{{#-first}} implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}}{{/formatSingleLine}} {

{{#allowableValues}}
{{#enumVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{{/additionalEnumTypeAnnotations}}
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#formatSingleLine}}enum class {{>common/model/enumName}}{{/formatSingleLine}}(
@get:JsonValue val value: {{{dataType}}},
) {
){{#vendorExtensions.x-implements}}{{#-first}} : {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {

{{#allowableValues}}
{{#enumVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,16 @@ public void sealedScenarios(String apiFile, Map<String, String> definitions) {
assertFileContains(path + file, check));
}

@Test
void testEnumXimplements() {

var codegen = new JavaMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/enum-implements.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "model/Type.java", "public enum Type implements java.io.Serializable {");
}

@Test
void testPascalCaseInMethodName() {

Expand All @@ -1630,7 +1640,8 @@ public EmployeeWithMapOfEnum putProjectRoleItem(String key, EmployeeWithMapOfEnu
"""
);
}


@Test
void testDateTimeFormat() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put("dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1559,50 +1559,50 @@ static Stream<Arguments> sealedScenarios() {
arguments("oneOf_polymorphismAndInheritance.yml", Map.of(
"Bar.kt", """
class Bar(

@Nullable
@JsonProperty(JSON_PROPERTY_ID)
id: String? = null,

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_BAR_PROP_A)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var barPropA: String? = null,

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_FOO_PROP_B)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var fooPropB: String? = null,

@field:Nullable
@field:Valid
@field:JsonProperty(JSON_PROPERTY_FOO)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var foo: FooRefOrValue? = null,

/**
* When sub-classing, this defines the sub-class Extensible name
*/
@Nullable
@JsonProperty(JSON_PROPERTY_AT_TYPE)
atType: String? = null,

/**
* Hyperlink reference
*/
@Nullable
@JsonProperty(JSON_PROPERTY_HREF)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
href: String? = null,

/**
* A URI to a JSON-Schema file that defines additional attributes and relationships
*/
@Nullable
@JsonProperty(JSON_PROPERTY_AT_SCHEMA_LOCATION)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
atSchemaLocation: String? = null,

/**
* When sub-classing, this defines the super-class
*/
Expand All @@ -1614,50 +1614,50 @@ class Bar(
""",
"Banana.kt", """
data class Banana(

@field:NotNull
@field:JsonProperty(JSON_PROPERTY_LENGTH)
var length: Int,

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_FRUIT_TYPE)
override var fruitType: FruitType? = null,
) : Fruit {
""",
"Entity.kt", """
open class Entity(

/**
* When sub-classing, this defines the sub-class Extensible name
*/
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_AT_TYPE)
open var atType: String? = null,

/**
* Hyperlink reference
*/
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_HREF)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var href: String? = null,

/**
* unique identifier
*/
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_ID)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var id: String? = null,

/**
* A URI to a JSON-Schema file that defines additional attributes and relationships
*/
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_AT_SCHEMA_LOCATION)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var atSchemaLocation: String? = null,

/**
* When sub-classing, this defines the super-class
*/
Expand All @@ -1670,7 +1670,7 @@ open class Entity(
arguments("oneOf_additionalProperties.yml", Map.of(
"SchemaA.kt", """
data class SchemaA(

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_PROP_A)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
Expand All @@ -1687,7 +1687,7 @@ data class SchemaA(
arguments("oneOf_primitive.yml", Map.of(
"Child.kt", """
data class Child(

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_NAME)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
Expand All @@ -1701,32 +1701,32 @@ data class Child(
"Fruit.kt", "interface Fruit {",
"Banana.kt", """
data class Banana(

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_LENGTH_CM)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var lengthCm: BigDecimal? = null,

@Nullable
@JsonProperty(JSON_PROPERTY_FRUIT_TYPE)
override var fruitType: String? = null,
) : Fruit {
""",
"Apple.kt", """
data class Apple(

@field:Nullable
@field:Pattern(regexp = "^[a-zA-Z\\\\s]*$")
@field:JsonProperty(JSON_PROPERTY_CULTIVAR)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var cultivar: String? = null,

@field:Nullable
@field:Pattern(regexp = "/^[A-Z\\\\s]*$/i")
@field:JsonProperty(JSON_PROPERTY_ORIGIN)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var origin: String? = null,

@Nullable
@JsonProperty(JSON_PROPERTY_FRUIT_TYPE)
override var fruitType: String? = null,
Expand All @@ -1738,7 +1738,7 @@ data class Apple(
"Fruit.kt", "interface Fruit",
"Grape.kt", """
data class Grape(

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_COLOR)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
Expand All @@ -1747,7 +1747,7 @@ data class Grape(
""",
"Apple.kt", """
data class Apple(

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_KIND)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
Expand Down Expand Up @@ -1795,4 +1795,20 @@ suspend fun browseSearchOrders(
"""
);
}

@Test
void testEnumXimplements() {

var codegen = new KotlinMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/enum-implements.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "model/Type.kt",
"""
enum class Type(
@get:JsonValue val value: String,
) : java.io.Serializable {
"""
);
}
}
33 changes: 33 additions & 0 deletions openapi-generator/src/test/resources/3_0/enum-implements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.0
info:
title: Sample API
description: API description in Markdown.
version: 1.0.0
paths:
/ponies:
get:
summary: Returns all animals.
description: Optional extended description in Markdown.
responses:
200:
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pony'
components:
schemas:
Pony:
type: object
properties:
type:
$ref: '#/components/schemas/Type'
Type:
type: string
x-implements: java.io.Serializable
enum:
- Earth
- Pegasi
- Unicorn
Loading