Skip to content

Issue: #6268 add default values support to Avro schema generator with related test cases #21226

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions bin/configs/avro-schema-issue6268.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generatorName: avro-schema
outputDir: samples/openapi3/schema/petstore/avro-schema-issue6268
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR. is there any command to validate the output?

what about adding a github workflow to do that?

ref: https://github.com/OpenAPITools/openapi-generator/tree/master/.github/workflows

inputSpec: modules/openapi-generator/src/test/resources/3_0/issue6268.yaml
templateDir: modules/openapi-generator/src/main/resources/avro-schema
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.FilenameUtils;
Expand All @@ -25,6 +26,7 @@
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -153,6 +155,25 @@ public void processOpts() {
}
}

/**
* Return the default value of the property
*
* @param p OpenAPI property object
* @return string presentation of the default value of the property
*/
@Override
public String toDefaultValue(Schema p) {
if (p.getDefault() == null) {
return null;
}

if (ModelUtils.isDateSchema(p) || ModelUtils.isDateTimeSchema(p) || ModelUtils.isStringSchema(p)) {
return "\"" + p.getDefault().toString() + "\"";
}

return p.getDefault().toString();
}

@Override
public CodegenType getTag() {
return CodegenType.SCHEMA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
{{#vars}}
{
"name": "{{baseName}}",
"type": {{^required}}["null", {{/required}}{{>typeProperty}}{{^required}}]{{/required}},
"doc": "{{{description}}}"{{^required}},
"default": null{{/required}}
"type": {{^defaultValue}}{{^required}}["null", {{/required}}{{>typeProperty}}{{^required}}]{{/required}}{{/defaultValue}}{{#defaultValue}}{{^required}}[{{/required}}{{>typeProperty}}{{^required}}, "null"]{{/required}}{{/defaultValue}},
"doc": "{{{description}}}"{{#defaultValue}},
"default": {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{^required}},
"default": null{{/required}}{{/defaultValue}}
}{{^-last}},{{/-last}}
{{/vars}}
]
58 changes: 58 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue6268.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
openapi: 3.0.3
info:
title: Issue 18345
description: ''
version: 1.0.0
paths:
/dummy:
post:
description: ''
operationId: uploadFile
requestBody:
content:
application/octet-stream:
schema:
type: string
format: binary
responses:
'200':
description: successful operation
components:
schemas:
SampleModelToTestAvroDefaultValues:
type: object
required:
- tagsRequired
- tagsRequiredWithDefault
properties:
name:
type: string
default: 'defaultName'
age:
type: integer
default: 25
isActive:
type: boolean
default: true
createdAt:
type: string
format: date-time
default: '2023-01-01T00:00:00Z'
tagsNotRequired:
type: array
items:
type: string
tagsNotRequiredWithDefault:
type: array
default: ['defaultTag']
items:
type: string
tagsRequired:
type: array
items:
type: string
tagsRequiredWithDefault:
type: array
default: ['defaultTag']
items:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SampleModelToTestAvroDefaultValues.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.14.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"namespace": "model",
"type": "record",
"doc": "",
"name": "SampleModelToTestAvroDefaultValues",
"fields": [
{
"name": "name",
"type": ["string", "null"],
"doc": "",
"default": "defaultName"
},
{
"name": "age",
"type": ["int", "null"],
"doc": "",
"default": 25
},
{
"name": "isActive",
"type": ["boolean", "null"],
"doc": "",
"default": true
},
{
"name": "createdAt",
"type": ["string", "null"],
"doc": "",
"default": "2023-01-01T00:00Z"
},
{
"name": "tagsNotRequired",
"type": ["null", {
"type": "array",
"items": "string"
}],
"doc": "",
"default": null
},
{
"name": "tagsNotRequiredWithDefault",
"type": [{
"type": "array",
"items": "string"
}, "null"],
"doc": "",
"default": ["defaultTag"]
},
{
"name": "tagsRequired",
"type": {
"type": "array",
"items": "string"
},
"doc": ""
},
{
"name": "tagsRequiredWithDefault",
"type": {
"type": "array",
"items": "string"
},
"doc": "",
"default": ["defaultTag"]
}
]

}
4 changes: 2 additions & 2 deletions samples/openapi3/schema/petstore/avro-schema/Order.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
},
{
"name": "complete",
"type": ["null", "boolean"],
"type": ["boolean", "null"],
"doc": "",
"default": null
"default": false
}
]

Expand Down
Loading