Skip to content

Commit 4f3eb90

Browse files
[dart] fix: handle multiline x-enum-descriptions (#24881)
1 parent b391b2b commit 4f3eb90

17 files changed

Lines changed: 238 additions & 70 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.openapitools.codegen.languages;
22

3+
import com.google.common.collect.ImmutableMap;
34
import com.google.common.collect.Sets;
5+
import com.samskivert.mustache.Mustache.Lambda;
46
import io.swagger.v3.oas.models.OpenAPI;
57
import io.swagger.v3.oas.models.Operation;
68
import io.swagger.v3.oas.models.media.Schema;
@@ -18,6 +20,8 @@
1820
import org.openapitools.codegen.model.ModelsMap;
1921
import org.openapitools.codegen.model.OperationMap;
2022
import org.openapitools.codegen.model.OperationsMap;
23+
import org.openapitools.codegen.templating.mustache.IndentedLambda;
24+
import org.openapitools.codegen.templating.mustache.PrefixLambda;
2125
import org.openapitools.codegen.utils.ModelUtils;
2226
import org.slf4j.Logger;
2327
import org.slf4j.LoggerFactory;
@@ -351,6 +355,13 @@ public void processOpts() {
351355
}
352356
}
353357

358+
@Override
359+
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
360+
return super.addMustacheLambdas()
361+
.put("dartdoc", new PrefixLambda(" * ", false))
362+
.put("dartdoc_2", new IndentedLambda(2, " ", " * ", false, false));
363+
}
364+
354365
@Override
355366
protected boolean needToImport(String type) {
356367
// Import everything, unless it is from dart:core.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2026 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openapitools.codegen.templating.mustache;
18+
19+
import com.samskivert.mustache.Mustache;
20+
import com.samskivert.mustache.Template;
21+
22+
import java.io.IOException;
23+
import java.io.Writer;
24+
25+
/**
26+
* Prefixes rendered fragment lines without applying additional indentation.
27+
*/
28+
public class PrefixLambda implements Mustache.Lambda {
29+
private final String prefix;
30+
private final boolean prefixFirstLine;
31+
32+
public PrefixLambda(String prefix, boolean prefixFirstLine) {
33+
this.prefix = prefix;
34+
this.prefixFirstLine = prefixFirstLine;
35+
}
36+
37+
@Override
38+
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
39+
String text = fragment.execute();
40+
if (text == null || text.isEmpty()) {
41+
return;
42+
}
43+
44+
String[] lines = text.split("\n", -1);
45+
StringBuilder sb = new StringBuilder();
46+
for (int i = 0; i < lines.length; i++) {
47+
if (prefixFirstLine || i > 0) {
48+
sb.append(prefix);
49+
}
50+
sb.append(lines[i]);
51+
if (i < lines.length - 1) {
52+
sb.append("\n");
53+
}
54+
}
55+
56+
writer.write(sb.toString());
57+
}
58+
}
59+

‎modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/enum.mustache‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ part '{{classFilename}}.g.dart';
1313
class {{classname}} extends EnumClass {
1414
1515
{{#allowableValues}}
16-
{{#enumVars}}
17-
{{#enumDescription}}
18-
/// {{{.}}}
19-
{{/enumDescription}}
16+
{{#enumVars}}
17+
{{#enumDescription}}
18+
/**
19+
* {{#lambda.dartdoc_2}}{{{.}}}{{/lambda.dartdoc_2}}
20+
*/
21+
{{/enumDescription}}
2022
@BuiltValueEnumConst({{#isInteger}}wireNumber: {{{value}}}{{/isInteger}}{{#isLong}}wireNumber: {{{value}}}{{/isLong}}{{^isInteger}}{{^isLong}}wireName: r{{{value}}}{{/isLong}}{{/isInteger}}{{#enumUnknownDefaultCase}}{{#-last}}, fallback: true{{/-last}}{{/enumUnknownDefaultCase}})
2123
static const {{classname}} {{name}} = _${{name}};
22-
{{/enumVars}}
24+
{{/enumVars}}
2325
{{/allowableValues}}
2426

2527
static Serializer<{{classname}}> get serializer => _${{#lambda.camelcase}}{{{classname}}}{{/lambda.camelcase}}Serializer;

‎modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/enum_inline.mustache‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
class {{{enumName}}} extends EnumClass {
88
99
{{#allowableValues}}
10-
{{#enumVars}}
11-
{{#enumDescription}}
12-
/// {{{.}}}
13-
{{/enumDescription}}
10+
{{#enumVars}}
11+
{{#enumDescription}}
12+
/**
13+
* {{#lambda.dartdoc_2}}{{{.}}}{{/lambda.dartdoc_2}}
14+
*/
15+
{{/enumDescription}}
1416
@BuiltValueEnumConst({{#isInteger}}wireNumber: {{{value}}}{{/isInteger}}{{#isLong}}wireNumber: {{{value}}}{{/isLong}}{{^isInteger}}{{^isLong}}wireName: r{{{value}}}{{/isLong}}{{/isInteger}}{{#enumUnknownDefaultCase}}{{#-last}}, fallback: true{{/-last}}{{/enumUnknownDefaultCase}})
1517
static const {{{enumName}}} {{name}} = _${{#lambda.camelcase}}{{{enumName}}}{{/lambda.camelcase}}_{{name}};
16-
{{/enumVars}}
18+
{{/enumVars}}
1719
{{/allowableValues}}
1820

1921
static Serializer<{{{enumName}}}> get serializer => _${{#lambda.camelcase}}{{{enumName}}}{{/lambda.camelcase}}Serializer;

‎modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/enum.mustache‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ enum {{{classname}}} {
1010
{{#allowableValues}}
1111
{{#enumVars}}
1212
{{^isNull}}
13-
{{#enumDescription}}
14-
/// {{{.}}}
15-
{{/enumDescription}}
16-
@JsonValue({{#isString}}r{{/isString}}{{{value}}})
17-
{{{name}}}({{^isString}}'{{/isString}}{{#isString}}r{{/isString}}{{{value}}}{{^isString}}'{{/isString}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
13+
{{#enumDescription}}
14+
/**
15+
* {{#lambda.dartdoc_2}}{{{.}}}{{/lambda.dartdoc_2}}
16+
*/
17+
{{/enumDescription}}
18+
@JsonValue({{#isString}}r{{/isString}}{{{value}}})
19+
{{{name}}}({{^isString}}'{{/isString}}{{#isString}}r{{/isString}}{{{value}}}{{^isString}}'{{/isString}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
1820
{{/isNull}}
1921
{{/enumVars}}
2022
{{/allowableValues}}

‎modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/enum_inline.mustache‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ enum {{{ enumName }}} {
1010
{{#enumVars}}
1111
{{^isNull}}
1212
{{#enumDescription}}
13-
/// {{{.}}}
13+
/**
14+
* {{#lambda.dartdoc}}{{{.}}}{{/lambda.dartdoc}}
15+
*/
1416
{{/enumDescription}}
1517
@JsonValue({{#isString}}r{{/isString}}{{{value}}})
1618
{{{name}}}({{^isString}}'{{/isString}}{{#isString}}r{{/isString}}{{{value}}}{{^isString}}'{{/isString}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}

‎modules/openapi-generator/src/test/resources/3_0/dart/petstore-with-fake-endpoints-models-for-testing.yaml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,16 @@ paths:
637637
- '-efg'
638638
- (xyz)
639639
default: '-efg'
640+
x-enum-descriptions:
641+
- |
642+
line1
643+
line2
644+
- |
645+
line3
646+
line4
647+
- |
648+
line5
649+
line6
640650
- name: enum_query_string_array
641651
in: query
642652
description: Query parameter enum test (string array)
@@ -1427,6 +1437,16 @@ components:
14271437
- placed
14281438
- approved
14291439
- delivered
1440+
x-enum-descriptions:
1441+
- |
1442+
line1
1443+
line2
1444+
- |
1445+
line3
1446+
line4
1447+
- |
1448+
line5
1449+
line6
14301450
complete:
14311451
type: boolean
14321452
default: false
@@ -2088,6 +2108,16 @@ components:
20882108
- ''
20892109
- 'value_one'
20902110
- 'value_two'
2111+
x-enum-descriptions:
2112+
- |
2113+
line1
2114+
line2
2115+
- |
2116+
line3
2117+
line4
2118+
- |
2119+
line5
2120+
line6
20912121
title: TestEnum
20922122
ObjectWithEnum:
20932123
type: object

‎samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/model_enum_class.dart‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import 'package:json_annotation/json_annotation.dart';
77

88
enum ModelEnumClass {
9-
@JsonValue(r'_abc')
10-
abc(r'_abc'),
11-
@JsonValue(r'-efg')
12-
efg(r'-efg'),
13-
@JsonValue(r'(xyz)')
14-
leftParenthesisXyzRightParenthesis(r'(xyz)'),
15-
@JsonValue(r'unknown_default_open_api')
16-
unknownDefaultOpenApi(r'unknown_default_open_api');
9+
@JsonValue(r'_abc')
10+
abc(r'_abc'),
11+
@JsonValue(r'-efg')
12+
efg(r'-efg'),
13+
@JsonValue(r'(xyz)')
14+
leftParenthesisXyzRightParenthesis(r'(xyz)'),
15+
@JsonValue(r'unknown_default_open_api')
16+
unknownDefaultOpenApi(r'unknown_default_open_api');
1717

1818
const ModelEnumClass(this.value);
1919

‎samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/order.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,25 @@ class Order {
140140

141141
/// Order Status
142142
enum OrderStatusEnum {
143+
/**
144+
* line1
145+
* line2
146+
*
147+
*/
143148
@JsonValue(r'placed')
144149
placed(r'placed'),
150+
/**
151+
* line3
152+
* line4
153+
*
154+
*/
145155
@JsonValue(r'approved')
146156
approved(r'approved'),
157+
/**
158+
* line5
159+
* line6
160+
*
161+
*/
147162
@JsonValue(r'delivered')
148163
delivered(r'delivered'),
149164
@JsonValue(r'unknown_default_open_api')

‎samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/outer_enum.dart‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import 'package:json_annotation/json_annotation.dart';
77

88
enum OuterEnum {
9-
@JsonValue(r'placed')
10-
placed(r'placed'),
11-
@JsonValue(r'approved')
12-
approved(r'approved'),
13-
@JsonValue(r'delivered')
14-
delivered(r'delivered'),
15-
@JsonValue(r'unknown_default_open_api')
16-
unknownDefaultOpenApi(r'unknown_default_open_api');
9+
@JsonValue(r'placed')
10+
placed(r'placed'),
11+
@JsonValue(r'approved')
12+
approved(r'approved'),
13+
@JsonValue(r'delivered')
14+
delivered(r'delivered'),
15+
@JsonValue(r'unknown_default_open_api')
16+
unknownDefaultOpenApi(r'unknown_default_open_api');
1717

1818
const OuterEnum(this.value);
1919

0 commit comments

Comments
 (0)