mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 12:40:53 +00:00
feat: add default values support to Avro schema generator with related test cases (#21226)
This commit is contained in:
parent
57bf6925bb
commit
91630b8591
4
bin/configs/avro-schema-issue6268.yaml
Normal file
4
bin/configs/avro-schema-issue6268.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
generatorName: avro-schema
|
||||
outputDir: samples/openapi3/schema/petstore/avro-schema-issue6268
|
||||
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue6268.yaml
|
||||
templateDir: modules/openapi-generator/src/main/resources/avro-schema
|
@ -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;
|
||||
@ -25,6 +26,7 @@ import org.openapitools.codegen.meta.GeneratorMetadata;
|
||||
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;
|
||||
|
||||
@ -153,6 +155,25 @@ public class AvroSchemaCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
@ -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}}
|
||||
]
|
||||
|
@ -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
|
@ -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
|
@ -0,0 +1 @@
|
||||
SampleModelToTestAvroDefaultValues.avsc
|
@ -0,0 +1 @@
|
||||
7.14.0-SNAPSHOT
|
@ -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"]
|
||||
}
|
||||
]
|
||||
|
||||
}
|
@ -44,9 +44,9 @@
|
||||
},
|
||||
{
|
||||
"name": "complete",
|
||||
"type": ["null", "boolean"],
|
||||
"type": ["boolean", "null"],
|
||||
"doc": "",
|
||||
"default": null
|
||||
"default": false
|
||||
}
|
||||
]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user