Skip to content

Commit 7eb3dd0

Browse files
[Kotlin Spring] fix: Omit example field if not set (OpenAPITools#24228)
* fix: Omit example field if not set. Fixes OpenAPITools#24227. * fix: update test expectations
1 parent 4b128f4 commit 7eb3dd0

113 files changed

Lines changed: 529 additions & 506 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ public void setEnumPropertyNaming(final String enumPropertyNamingType) {
384384
}
385385
}
386386

387+
@Override
388+
public String toExampleValue(Schema schema) {
389+
if (schema.getExample() != null) {
390+
return super.toExampleValue(schema);
391+
}
392+
return null;
393+
}
394+
387395
/**
388396
* returns the OpenAPI type for the property
389397
*

‎modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,24 +788,31 @@ public void useTargetOnInterfaceAnnotations() throws IOException {
788788
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
789789
"@Schema(example = \"null\", description = \"\")"
790790
);
791-
assertFileContains(
791+
assertFileNotContains(
792792
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
793793
"@get:Schema(example = \"null\", description = \"\")"
794794
);
795+
assertFileContains(
796+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
797+
"@get:Schema(description = \"\")"
798+
);
795799
assertFileNotContains(
796800
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
797801
"@Schema(example = \"null\", requiredMode = Schema.RequiredMode.REQUIRED, description = \"\")"
798802
);
799-
assertFileContains(
803+
assertFileNotContains(
800804
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
801805
"@get:Schema(example = \"null\", requiredMode = Schema.RequiredMode.REQUIRED, description = \"\")"
802806
);
807+
assertFileContains(
808+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
809+
"@get:Schema(requiredMode = Schema.RequiredMode.REQUIRED, description = \"\")"
810+
);
803811
}
804812

805813
@Test(description = "use get Annotation use-site target on kotlin interface attributes (swagger1)")
806814
public void useTargetOnInterfaceAnnotationsWithSwagger1() throws IOException {
807815
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
808-
output.deleteOnExit();
809816
String outputPath = output.getAbsolutePath().replace('\\', '/');
810817

811818
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
@@ -823,18 +830,26 @@ public void useTargetOnInterfaceAnnotationsWithSwagger1() throws IOException {
823830
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
824831
"@ApiModelProperty(example = \"null\", value = \"\")"
825832
);
826-
assertFileContains(
833+
assertFileNotContains(
827834
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
828835
"@get:ApiModelProperty(example = \"null\", value = \"\")"
829836
);
837+
assertFileContains(
838+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
839+
"@get:ApiModelProperty(value = \"\")"
840+
);
830841
assertFileNotContains(
831842
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
832843
"@ApiModelProperty(example = \"null\", required = true, value = \"\")"
833844
);
834-
assertFileContains(
845+
assertFileNotContains(
835846
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
836847
"@get:ApiModelProperty(example = \"null\", required = true, value = \"\")"
837848
);
849+
assertFileContains(
850+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Animal.kt"),
851+
"@get:ApiModelProperty(required = true, value = \"\")"
852+
);
838853
}
839854

840855
@Test

‎samples/server/petstore/kotlin-spring-declarative-interface-bean-validation/src/main/kotlin/org/openapitools/model/Category.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import io.swagger.v3.oas.annotations.media.Schema
2323
*/
2424
data class Category(
2525

26-
@Schema(example = "null", description = "")
26+
@Schema(description = "")
2727
@field:JsonInclude(JsonInclude.Include.NON_NULL)
2828
@field:JsonSetter(nulls = Nulls.SKIP)
2929
@param:JsonProperty("id")
3030
@get:JsonProperty("id") val id: kotlin.Long? = null,
3131

3232
@get:Pattern(regexp="^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")
33-
@Schema(example = "null", description = "")
33+
@Schema(description = "")
3434
@field:JsonInclude(JsonInclude.Include.NON_NULL)
3535
@field:JsonSetter(nulls = Nulls.SKIP)
3636
@param:JsonProperty("name")

‎samples/server/petstore/kotlin-spring-declarative-interface-bean-validation/src/main/kotlin/org/openapitools/model/ModelApiResponse.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ import io.swagger.v3.oas.annotations.media.Schema
2424
*/
2525
data class ModelApiResponse(
2626

27-
@Schema(example = "null", description = "")
27+
@Schema(description = "")
2828
@field:JsonInclude(JsonInclude.Include.NON_NULL)
2929
@field:JsonSetter(nulls = Nulls.SKIP)
3030
@param:JsonProperty("code")
3131
@get:JsonProperty("code") val code: kotlin.Int? = null,
3232

33-
@Schema(example = "null", description = "")
33+
@Schema(description = "")
3434
@field:JsonInclude(JsonInclude.Include.NON_NULL)
3535
@field:JsonSetter(nulls = Nulls.SKIP)
3636
@param:JsonProperty("type")
3737
@get:JsonProperty("type") val type: kotlin.String? = null,
3838

39-
@Schema(example = "null", description = "")
39+
@Schema(description = "")
4040
@field:JsonInclude(JsonInclude.Include.NON_NULL)
4141
@field:JsonSetter(nulls = Nulls.SKIP)
4242
@param:JsonProperty("message")

‎samples/server/petstore/kotlin-spring-declarative-interface-bean-validation/src/main/kotlin/org/openapitools/model/Order.kt‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,37 @@ import io.swagger.v3.oas.annotations.media.Schema
2929
*/
3030
data class Order(
3131

32-
@Schema(example = "null", description = "")
32+
@Schema(description = "")
3333
@field:JsonInclude(JsonInclude.Include.NON_NULL)
3434
@field:JsonSetter(nulls = Nulls.SKIP)
3535
@param:JsonProperty("id")
3636
@get:JsonProperty("id") val id: kotlin.Long? = null,
3737

38-
@Schema(example = "null", description = "")
38+
@Schema(description = "")
3939
@field:JsonInclude(JsonInclude.Include.NON_NULL)
4040
@field:JsonSetter(nulls = Nulls.SKIP)
4141
@param:JsonProperty("petId")
4242
@get:JsonProperty("petId") val petId: kotlin.Long? = null,
4343

44-
@Schema(example = "null", description = "")
44+
@Schema(description = "")
4545
@field:JsonInclude(JsonInclude.Include.NON_NULL)
4646
@field:JsonSetter(nulls = Nulls.SKIP)
4747
@param:JsonProperty("quantity")
4848
@get:JsonProperty("quantity") val quantity: kotlin.Int? = null,
4949

50-
@Schema(example = "null", description = "")
50+
@Schema(description = "")
5151
@field:JsonInclude(JsonInclude.Include.NON_NULL)
5252
@field:JsonSetter(nulls = Nulls.SKIP)
5353
@param:JsonProperty("shipDate")
5454
@get:JsonProperty("shipDate") val shipDate: java.time.OffsetDateTime? = null,
5555

56-
@Schema(example = "null", description = "Order Status")
56+
@Schema(description = "Order Status")
5757
@field:JsonInclude(JsonInclude.Include.NON_NULL)
5858
@field:JsonSetter(nulls = Nulls.SKIP)
5959
@param:JsonProperty("status")
6060
@get:JsonProperty("status") val status: Order.Status? = null,
6161

62-
@Schema(example = "null", description = "")
62+
@Schema(description = "")
6363
@field:JsonInclude(JsonInclude.Include.NON_NULL)
6464
@field:JsonSetter(nulls = Nulls.SKIP)
6565
@param:JsonProperty("complete")

‎samples/server/petstore/kotlin-spring-declarative-interface-bean-validation/src/main/kotlin/org/openapitools/model/Pet.kt‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,31 @@ data class Pet(
3535
@param:JsonProperty("name")
3636
@get:JsonProperty("name", required = true) val name: kotlin.String,
3737

38-
@Schema(example = "null", required = true, description = "")
38+
@Schema(required = true, description = "")
3939
@param:JsonProperty("photoUrls")
4040
@get:JsonProperty("photoUrls", required = true) val photoUrls: kotlin.collections.List<kotlin.String>,
4141

42-
@Schema(example = "null", description = "")
42+
@Schema(description = "")
4343
@field:JsonInclude(JsonInclude.Include.NON_NULL)
4444
@field:JsonSetter(nulls = Nulls.SKIP)
4545
@param:JsonProperty("id")
4646
@get:JsonProperty("id") val id: kotlin.Long? = null,
4747

4848
@field:Valid
49-
@Schema(example = "null", description = "")
49+
@Schema(description = "")
5050
@field:JsonInclude(JsonInclude.Include.NON_NULL)
5151
@field:JsonSetter(nulls = Nulls.SKIP)
5252
@param:JsonProperty("category")
5353
@get:JsonProperty("category") val category: Category? = null,
5454

5555
@field:Valid
56-
@Schema(example = "null", description = "")
56+
@Schema(description = "")
5757
@field:JsonInclude(JsonInclude.Include.NON_NULL)
5858
@field:JsonSetter(nulls = Nulls.SKIP)
5959
@param:JsonProperty("tags")
6060
@get:JsonProperty("tags") val tags: kotlin.collections.List<Tag>? = null,
6161

62-
@Schema(example = "null", description = "pet status in the store")
62+
@Schema(description = "pet status in the store")
6363
@Deprecated(message = "")
6464
@field:JsonInclude(JsonInclude.Include.NON_NULL)
6565
@field:JsonSetter(nulls = Nulls.SKIP)

‎samples/server/petstore/kotlin-spring-declarative-interface-bean-validation/src/main/kotlin/org/openapitools/model/Tag.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import io.swagger.v3.oas.annotations.media.Schema
2323
*/
2424
data class Tag(
2525

26-
@Schema(example = "null", description = "")
26+
@Schema(description = "")
2727
@field:JsonInclude(JsonInclude.Include.NON_NULL)
2828
@field:JsonSetter(nulls = Nulls.SKIP)
2929
@param:JsonProperty("id")
3030
@get:JsonProperty("id") val id: kotlin.Long? = null,
3131

32-
@Schema(example = "null", description = "")
32+
@Schema(description = "")
3333
@field:JsonInclude(JsonInclude.Include.NON_NULL)
3434
@field:JsonSetter(nulls = Nulls.SKIP)
3535
@param:JsonProperty("name")

‎samples/server/petstore/kotlin-spring-declarative-interface-bean-validation/src/main/kotlin/org/openapitools/model/User.kt‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,49 @@ import io.swagger.v3.oas.annotations.media.Schema
2929
*/
3030
data class User(
3131

32-
@Schema(example = "null", description = "")
32+
@Schema(description = "")
3333
@field:JsonInclude(JsonInclude.Include.NON_NULL)
3434
@field:JsonSetter(nulls = Nulls.SKIP)
3535
@param:JsonProperty("id")
3636
@get:JsonProperty("id") val id: kotlin.Long? = null,
3737

38-
@Schema(example = "null", description = "")
38+
@Schema(description = "")
3939
@field:JsonInclude(JsonInclude.Include.NON_NULL)
4040
@field:JsonSetter(nulls = Nulls.SKIP)
4141
@param:JsonProperty("username")
4242
@get:JsonProperty("username") val username: kotlin.String? = null,
4343

44-
@Schema(example = "null", description = "")
44+
@Schema(description = "")
4545
@field:JsonInclude(JsonInclude.Include.NON_NULL)
4646
@field:JsonSetter(nulls = Nulls.SKIP)
4747
@param:JsonProperty("firstName")
4848
@get:JsonProperty("firstName") val firstName: kotlin.String? = null,
4949

50-
@Schema(example = "null", description = "")
50+
@Schema(description = "")
5151
@field:JsonInclude(JsonInclude.Include.NON_NULL)
5252
@field:JsonSetter(nulls = Nulls.SKIP)
5353
@param:JsonProperty("lastName")
5454
@get:JsonProperty("lastName") val lastName: kotlin.String? = null,
5555

56-
@Schema(example = "null", description = "")
56+
@Schema(description = "")
5757
@field:JsonInclude(JsonInclude.Include.NON_NULL)
5858
@field:JsonSetter(nulls = Nulls.SKIP)
5959
@param:JsonProperty("email")
6060
@get:JsonProperty("email") val email: kotlin.String? = null,
6161

62-
@Schema(example = "null", description = "")
62+
@Schema(description = "")
6363
@field:JsonInclude(JsonInclude.Include.NON_NULL)
6464
@field:JsonSetter(nulls = Nulls.SKIP)
6565
@param:JsonProperty("password")
6666
@get:JsonProperty("password") val password: kotlin.String? = null,
6767

68-
@Schema(example = "null", description = "")
68+
@Schema(description = "")
6969
@field:JsonInclude(JsonInclude.Include.NON_NULL)
7070
@field:JsonSetter(nulls = Nulls.SKIP)
7171
@param:JsonProperty("phone")
7272
@get:JsonProperty("phone") val phone: kotlin.String? = null,
7373

74-
@Schema(example = "null", description = "User Status")
74+
@Schema(description = "User Status")
7575
@field:JsonInclude(JsonInclude.Include.NON_NULL)
7676
@field:JsonSetter(nulls = Nulls.SKIP)
7777
@param:JsonProperty("userStatus")

‎samples/server/petstore/kotlin-spring-declarative-interface-reactive-coroutines/src/main/kotlin/org/openapitools/model/Category.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import io.swagger.v3.oas.annotations.media.Schema
2323
*/
2424
data class Category(
2525

26-
@Schema(example = "null", description = "")
26+
@Schema(description = "")
2727
@field:JsonInclude(JsonInclude.Include.NON_NULL)
2828
@field:JsonSetter(nulls = Nulls.SKIP)
2929
@param:JsonProperty("id")
3030
@get:JsonProperty("id") val id: kotlin.Long? = null,
3131

3232
@get:Pattern(regexp="^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")
33-
@Schema(example = "null", description = "")
33+
@Schema(description = "")
3434
@field:JsonInclude(JsonInclude.Include.NON_NULL)
3535
@field:JsonSetter(nulls = Nulls.SKIP)
3636
@param:JsonProperty("name")

‎samples/server/petstore/kotlin-spring-declarative-interface-reactive-coroutines/src/main/kotlin/org/openapitools/model/ModelApiResponse.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ import io.swagger.v3.oas.annotations.media.Schema
2424
*/
2525
data class ModelApiResponse(
2626

27-
@Schema(example = "null", description = "")
27+
@Schema(description = "")
2828
@field:JsonInclude(JsonInclude.Include.NON_NULL)
2929
@field:JsonSetter(nulls = Nulls.SKIP)
3030
@param:JsonProperty("code")
3131
@get:JsonProperty("code") val code: kotlin.Int? = null,
3232

33-
@Schema(example = "null", description = "")
33+
@Schema(description = "")
3434
@field:JsonInclude(JsonInclude.Include.NON_NULL)
3535
@field:JsonSetter(nulls = Nulls.SKIP)
3636
@param:JsonProperty("type")
3737
@get:JsonProperty("type") val type: kotlin.String? = null,
3838

39-
@Schema(example = "null", description = "")
39+
@Schema(description = "")
4040
@field:JsonInclude(JsonInclude.Include.NON_NULL)
4141
@field:JsonSetter(nulls = Nulls.SKIP)
4242
@param:JsonProperty("message")

0 commit comments

Comments
 (0)