Skip to content

Commit 91630b8

Browse files
author
Mostafa Aghajani
authored
feat: add default values support to Avro schema generator with related test cases (#21226)
1 parent 57bf692 commit 91630b8

File tree

9 files changed

+182
-5
lines changed

9 files changed

+182
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generatorName: avro-schema
2+
outputDir: samples/openapi3/schema/petstore/avro-schema-issue6268
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue6268.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/avro-schema

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.openapitools.codegen.languages;
1818

19+
import io.swagger.v3.oas.models.media.Schema;
1920
import lombok.Getter;
2021
import lombok.Setter;
2122
import org.apache.commons.io.FilenameUtils;
@@ -25,6 +26,7 @@
2526
import org.openapitools.codegen.meta.Stability;
2627
import org.openapitools.codegen.meta.features.*;
2728
import org.openapitools.codegen.model.ModelsMap;
29+
import org.openapitools.codegen.utils.ModelUtils;
2830
import org.slf4j.Logger;
2931
import org.slf4j.LoggerFactory;
3032

@@ -153,6 +155,25 @@ public void processOpts() {
153155
}
154156
}
155157

158+
/**
159+
* Return the default value of the property
160+
*
161+
* @param p OpenAPI property object
162+
* @return string presentation of the default value of the property
163+
*/
164+
@Override
165+
public String toDefaultValue(Schema p) {
166+
if (p.getDefault() == null) {
167+
return null;
168+
}
169+
170+
if (ModelUtils.isDateSchema(p) || ModelUtils.isDateTimeSchema(p) || ModelUtils.isStringSchema(p)) {
171+
return "\"" + p.getDefault().toString() + "\"";
172+
}
173+
174+
return p.getDefault().toString();
175+
}
176+
156177
@Override
157178
public CodegenType getTag() {
158179
return CodegenType.SCHEMA;

modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
{{#vars}}
33
{
44
"name": "{{baseName}}",
5-
"type": {{^required}}["null", {{/required}}{{>typeProperty}}{{^required}}]{{/required}},
6-
"doc": "{{{description}}}"{{^required}},
7-
"default": null{{/required}}
5+
"type": {{^defaultValue}}{{^required}}["null", {{/required}}{{>typeProperty}}{{^required}}]{{/required}}{{/defaultValue}}{{#defaultValue}}{{^required}}[{{/required}}{{>typeProperty}}{{^required}}, "null"]{{/required}}{{/defaultValue}},
6+
"doc": "{{{description}}}"{{#defaultValue}},
7+
"default": {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{^required}},
8+
"default": null{{/required}}{{/defaultValue}}
89
}{{^-last}},{{/-last}}
910
{{/vars}}
1011
]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Issue 18345
4+
description: ''
5+
version: 1.0.0
6+
paths:
7+
/dummy:
8+
post:
9+
description: ''
10+
operationId: uploadFile
11+
requestBody:
12+
content:
13+
application/octet-stream:
14+
schema:
15+
type: string
16+
format: binary
17+
responses:
18+
'200':
19+
description: successful operation
20+
components:
21+
schemas:
22+
SampleModelToTestAvroDefaultValues:
23+
type: object
24+
required:
25+
- tagsRequired
26+
- tagsRequiredWithDefault
27+
properties:
28+
name:
29+
type: string
30+
default: 'defaultName'
31+
age:
32+
type: integer
33+
default: 25
34+
isActive:
35+
type: boolean
36+
default: true
37+
createdAt:
38+
type: string
39+
format: date-time
40+
default: '2023-01-01T00:00:00Z'
41+
tagsNotRequired:
42+
type: array
43+
items:
44+
type: string
45+
tagsNotRequiredWithDefault:
46+
type: array
47+
default: ['defaultTag']
48+
items:
49+
type: string
50+
tagsRequired:
51+
type: array
52+
items:
53+
type: string
54+
tagsRequiredWithDefault:
55+
type: array
56+
default: ['defaultTag']
57+
items:
58+
type: string
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SampleModelToTestAvroDefaultValues.avsc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.14.0-SNAPSHOT
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"namespace": "model",
3+
"type": "record",
4+
"doc": "",
5+
"name": "SampleModelToTestAvroDefaultValues",
6+
"fields": [
7+
{
8+
"name": "name",
9+
"type": ["string", "null"],
10+
"doc": "",
11+
"default": "defaultName"
12+
},
13+
{
14+
"name": "age",
15+
"type": ["int", "null"],
16+
"doc": "",
17+
"default": 25
18+
},
19+
{
20+
"name": "isActive",
21+
"type": ["boolean", "null"],
22+
"doc": "",
23+
"default": true
24+
},
25+
{
26+
"name": "createdAt",
27+
"type": ["string", "null"],
28+
"doc": "",
29+
"default": "2023-01-01T00:00Z"
30+
},
31+
{
32+
"name": "tagsNotRequired",
33+
"type": ["null", {
34+
"type": "array",
35+
"items": "string"
36+
}],
37+
"doc": "",
38+
"default": null
39+
},
40+
{
41+
"name": "tagsNotRequiredWithDefault",
42+
"type": [{
43+
"type": "array",
44+
"items": "string"
45+
}, "null"],
46+
"doc": "",
47+
"default": ["defaultTag"]
48+
},
49+
{
50+
"name": "tagsRequired",
51+
"type": {
52+
"type": "array",
53+
"items": "string"
54+
},
55+
"doc": ""
56+
},
57+
{
58+
"name": "tagsRequiredWithDefault",
59+
"type": {
60+
"type": "array",
61+
"items": "string"
62+
},
63+
"doc": "",
64+
"default": ["defaultTag"]
65+
}
66+
]
67+
68+
}

samples/openapi3/schema/petstore/avro-schema/Order.avsc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
},
4545
{
4646
"name": "complete",
47-
"type": ["null", "boolean"],
47+
"type": ["boolean", "null"],
4848
"doc": "",
49-
"default": null
49+
"default": false
5050
}
5151
]
5252

0 commit comments

Comments
 (0)