Skip to content

Commit 98e8276

Browse files
committed
Escape true and false reserved words in java generator
Fixed #2455
1 parent d5d19c3 commit 98e8276

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ protected AbstractMicronautJavaCodegen() {
342342

343343
// Add reserved words
344344
var micronautReservedWords = List.of(
345+
// lost words in openapi generator
346+
"true", "false",
345347
// special words
346348
"Object", "List", "File", "OffsetDateTime", "LocalDate", "LocalTime",
347349
"Client", "Format", "QueryValue", "QueryParam", "PathVariable", "Header", "Cookie",

openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,4 +2357,60 @@ void testEnumExtensions() {
23572357
NUM("NUM"),
23582358
""");
23592359
}
2360+
2361+
@Test
2362+
void testTrueFalse() {
2363+
2364+
var codegen = new JavaMicronautClientCodegen();
2365+
String outputPathApi = generateFiles(codegen, "src/test/resources/3_0/true-false.yml");
2366+
2367+
String path = outputPathApi + "src/main/java/org/openapitools/";
2368+
assertFileContains(path + "api/OrderApi.java", """
2369+
/**
2370+
* getOrderById
2371+
*
2372+
* @param _true (optional)
2373+
* @param _false (optional)
2374+
* @param _null (optional)
2375+
* @param _boolean (optional)
2376+
* @param propertyClass (optional)
2377+
*
2378+
* @return OK (status code 200)
2379+
*/
2380+
@Get("/orders/{id}")
2381+
Mono<@NotNull String> getOrderById(
2382+
@QueryValue("true") @Nullable String _true,
2383+
@QueryValue("false") @Nullable String _false,
2384+
@QueryValue("null") @Nullable String _null,
2385+
@QueryValue("boolean") @Nullable String _boolean,
2386+
@QueryValue("class") @Nullable String propertyClass
2387+
);
2388+
""");
2389+
2390+
assertFileContains(path + "model/CDBAttributeUsageUiBoolean.java", """
2391+
/**
2392+
* a.
2393+
*/
2394+
@NotNull
2395+
@JsonProperty(JSON_PROPERTY_TRUE)
2396+
@JsonInclude(content = JsonInclude.Include.ALWAYS)
2397+
private Map<String, Object> _true;
2398+
2399+
/**
2400+
* a.
2401+
*/
2402+
@NotNull
2403+
@JsonProperty(JSON_PROPERTY_FALSE)
2404+
@JsonInclude(content = JsonInclude.Include.ALWAYS)
2405+
private Map<String, Object> _false;
2406+
2407+
/**
2408+
* a.
2409+
*/
2410+
@NotNull
2411+
@JsonProperty(JSON_PROPERTY_NULL)
2412+
@JsonInclude(content = JsonInclude.Include.ALWAYS)
2413+
private Map<String, Object> _null;
2414+
""");
2415+
}
23602416
}

openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,4 +2728,60 @@ void testEnumExtensions() {
27282728
NUM("NUM"),
27292729
""");
27302730
}
2731+
2732+
@Test
2733+
void testTrueFalse() {
2734+
2735+
var codegen = new KotlinMicronautClientCodegen();
2736+
String outputPathApi = generateFiles(codegen, "src/test/resources/3_0/true-false.yml");
2737+
2738+
String path = outputPathApi + "src/main/kotlin/org/openapitools/";
2739+
assertFileContains(path + "api/OrderApi.kt", """
2740+
/**
2741+
* getOrderById
2742+
*
2743+
* @param true (optional)
2744+
* @param false (optional)
2745+
* @param null (optional)
2746+
* @param boolean (optional)
2747+
* @param propertyClass (optional)
2748+
*
2749+
* @return OK (status code 200)
2750+
*/
2751+
@Get("/orders/{id}")
2752+
fun getOrderById(
2753+
@QueryValue("true") @Nullable `true`: String? = null,
2754+
@QueryValue("false") @Nullable `false`: String? = null,
2755+
@QueryValue("null") @Nullable `null`: String? = null,
2756+
@QueryValue("boolean") @Nullable boolean: String? = null,
2757+
@QueryValue("class") @Nullable propertyClass: String? = null,
2758+
): Mono<String>
2759+
""");
2760+
2761+
assertFileContains(path + "model/CDBAttributeUsageUiBoolean.kt", """
2762+
/**
2763+
* a.
2764+
*/
2765+
@field:NotNull
2766+
@field:JsonProperty(JSON_PROPERTY_TRUE)
2767+
@field:JsonInclude(content = JsonInclude.Include.ALWAYS)
2768+
var `true`: Map<String, Any?>,
2769+
2770+
/**
2771+
* a.
2772+
*/
2773+
@field:NotNull
2774+
@field:JsonProperty(JSON_PROPERTY_FALSE)
2775+
@field:JsonInclude(content = JsonInclude.Include.ALWAYS)
2776+
var `false`: Map<String, Any?>,
2777+
2778+
/**
2779+
* a.
2780+
*/
2781+
@field:NotNull
2782+
@field:JsonProperty(JSON_PROPERTY_NULL)
2783+
@field:JsonInclude(content = JsonInclude.Include.ALWAYS)
2784+
var `null`: Map<String, Any?>,
2785+
""");
2786+
}
27312787
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
openapi: 3.1.0
2+
info:
3+
title: title
4+
description: description
5+
version: ''
6+
servers:
7+
- url: /
8+
paths:
9+
'/orders/{id}':
10+
get:
11+
tags:
12+
- Order
13+
operationId: getOrderById
14+
parameters:
15+
- name: 'true'
16+
in: query
17+
schema:
18+
type: string
19+
- name: 'false'
20+
in: query
21+
schema:
22+
type: string
23+
- name: 'null'
24+
in: query
25+
schema:
26+
type: string
27+
- name: boolean
28+
in: query
29+
schema:
30+
type: string
31+
- name: class
32+
in: query
33+
schema:
34+
type: string
35+
responses:
36+
'200':
37+
description: OK
38+
content:
39+
application/json:
40+
schema:
41+
type: string
42+
components:
43+
schemas:
44+
CDBAttributeUsage:
45+
type: object
46+
description: g
47+
properties:
48+
instance:
49+
type: boolean
50+
description: d
51+
instanceOrder:
52+
type: integer
53+
format: int64
54+
description: d
55+
ui:
56+
type: object
57+
description: d
58+
properties:
59+
group:
60+
type: object
61+
description: d
62+
properties:
63+
id:
64+
type: string
65+
description: d.
66+
required:
67+
- id
68+
boolean:
69+
type: object
70+
description: d
71+
properties:
72+
'true':
73+
$ref: '#/components/schemas/MLString'
74+
description: x
75+
'false':
76+
$ref: '#/components/schemas/MLString'
77+
description: x
78+
'null':
79+
$ref: '#/components/schemas/MLString'
80+
description: x
81+
toggle:
82+
type: object
83+
description: x
84+
properties:
85+
'true':
86+
$ref: '#/components/schemas/MLString'
87+
description: f
88+
'false':
89+
$ref: '#/components/schemas/MLString'
90+
description: f
91+
required:
92+
- 'true'
93+
- 'false'
94+
required:
95+
- 'null'
96+
- 'true'
97+
- 'false'
98+
- toggle
99+
required:
100+
- boolean
101+
- group
102+
unit:
103+
type: boolean
104+
description: v
105+
unitOrder:
106+
type: integer
107+
format: int64
108+
description: a
109+
MLString:
110+
type: object
111+
additionalProperties: true
112+
description: a.

0 commit comments

Comments
 (0)