diff --git a/bin/configs/avro-schema-issue6268.yaml b/bin/configs/avro-schema-issue6268.yaml new file mode 100644 index 000000000000..8061ee3d7a4a --- /dev/null +++ b/bin/configs/avro-schema-issue6268.yaml @@ -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 diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java index ea2d8efd9542..572ec6f64777 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java @@ -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.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 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; diff --git a/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache b/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache index e6967d322a3a..eb44fc285dbe 100644 --- a/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache @@ -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}} ] diff --git a/modules/openapi-generator/src/test/resources/3_0/issue6268.yaml b/modules/openapi-generator/src/test/resources/3_0/issue6268.yaml new file mode 100644 index 000000000000..ce909ec3084f --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/issue6268.yaml @@ -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 diff --git a/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator-ignore b/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator-ignore new file mode 100644 index 000000000000..7484ee590a38 --- /dev/null +++ b/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator-ignore @@ -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 diff --git a/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator/FILES b/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator/FILES new file mode 100644 index 000000000000..fb590f3217a3 --- /dev/null +++ b/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator/FILES @@ -0,0 +1 @@ +SampleModelToTestAvroDefaultValues.avsc diff --git a/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator/VERSION b/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator/VERSION new file mode 100644 index 000000000000..4c631cf217a2 --- /dev/null +++ b/samples/openapi3/schema/petstore/avro-schema-issue6268/.openapi-generator/VERSION @@ -0,0 +1 @@ +7.14.0-SNAPSHOT diff --git a/samples/openapi3/schema/petstore/avro-schema-issue6268/SampleModelToTestAvroDefaultValues.avsc b/samples/openapi3/schema/petstore/avro-schema-issue6268/SampleModelToTestAvroDefaultValues.avsc new file mode 100644 index 000000000000..1ff0ad5289e0 --- /dev/null +++ b/samples/openapi3/schema/petstore/avro-schema-issue6268/SampleModelToTestAvroDefaultValues.avsc @@ -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"] + } + ] + +} diff --git a/samples/openapi3/schema/petstore/avro-schema/Order.avsc b/samples/openapi3/schema/petstore/avro-schema/Order.avsc index 1f1f7564db93..2f1c1ce518dc 100644 --- a/samples/openapi3/schema/petstore/avro-schema/Order.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/Order.avsc @@ -44,9 +44,9 @@ }, { "name": "complete", - "type": ["null", "boolean"], + "type": ["boolean", "null"], "doc": "", - "default": null + "default": false } ]