OpenAPITools / OpenAPITools/openapi-generator

[BUG] The generator generates invalid value for the "example" field

Open
#5,710 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Doc: AsciiDoc Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Sometimes, the generated value for "example" fields are wrong.

openapi-generator version

4.2.3

OpenAPI declaration file content or url
openapi: "3.0.0"

info:
  title: Test
  version: 1.0.0

paths:
  /test:
    post:
      summary: A simple endpoint.
      description: A more detailed explanation of our simple endpoint.
      parameters:
      - in: header
        name: X-Custom
        required: true
        schema:
          $ref: '#/components/schemas/Custom'
      responses:
        '200':
          description: OK

components:
  schemas:
    Custom:
      description: Some description.
      type: string
      format: fubar
      pattern: '^(foo|bar|baz)+$'
      example: foofoobazbarfoo
Command line used for generation
openapi-generator generate -i sample.yaml -g asciidoc -v
Steps to reproduce
  1. Save the given Open API spec in a file named sample.yaml
  2. Run openapi-generator generate -i sample.yaml -g asciidoc -v
  3. The example field should have the value foofoobazbarfoo but it has xCustom_example:
{
  "licenseUrl" : "http://apache.org/licenses/LICENSE-2.0.html",
  "appVersion" : "1.0.0",
  "generatedYear" : "2020",
  "generatorClass" : "org.openapitools.codegen.languages.AsciidocDocumentationCodegen",
  "openAPI" : {
    "openapi" : "3.0.0",
    "info" : {
      "title" : "Test",
      "version" : "1.0.0"
    },
    "servers" : [ {
      "url" : "/"
    } ],
    "paths" : {
      "/test" : {
        "post" : {
          "summary" : "A simple endpoint.",
          "description" : "A more detailed explanation of our simple endpoint.",
          "parameters" : [ {
            "name" : "X-Custom",
            "in" : "header",
            "required" : true,
            "style" : "simple",
            "explode" : false,
            "schema" : {
              "$ref" : "#/components/schemas/Custom"
            }
          } ],
          "responses" : {
            "200" : {
              "description" : "OK"
            }
          }
        }
      }
    },
    "components" : {
      "schemas" : {
        "Custom" : {
          "pattern" : "^(foo|bar|baz)+$",
          "type" : "string",
          "description" : "Some description.",
          "format" : "fubar",
          "example" : "foofoobazbarfoo"
        }
      }
    }
  },
  "scheme" : "http",
  "modelPackage" : "",
  "groupId" : "org.openapitools",
  "gitHost" : "github.com",
  "licenseInfo" : "All rights reserved",
  "invokerPackage" : "org.openapitools.client",
  "apiFolder" : "",
  "generateApis" : true,
  "generateModelDocs" : true,
  "generateModelTests" : true,
  "basePathWithoutHost" : "",
  "generateApiTests" : true,
  "lambda" : {
    "lowercase" : { },
    "uppercase" : { },
    "titlecase" : { },
    "camelcase" : { },
    "indented" : { },
    "indented_8" : { },
    "indented_12" : { },
    "indented_16" : { }
  },
  "generateModels" : true,
  "servers" : [ {
    "url" : "/",
    "variables" : [ ]
  } ],
  "infoUrl" : "https://openapi-generator.tech",
  "inputSpec" : "sample.yaml",
  "host" : "localhost",
  "artifactId" : "openapi-client",
  "hideGenerationTimestamp" : true,
  "snippetlink" : { },
  "unescapedAppDescription" : "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)",
  "models" : [ ],
  "artifactVersion" : "1.0.0",
  "appName" : "Test",
  "snippetinclude" : { },
  "appDescription" : "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)",
  "contextPath" : "",
  "generateApiDocs" : true,
  "generatorVersion" : "4.2.3",
  "releaseNote" : "Minor update",
  "version" : "1.0.0",
  "apiInfo" : {
    "apis" : [ {
      "appVersion" : "1.0.0",
      "generatorClass" : "org.openapitools.codegen.languages.AsciidocDocumentationCodegen",
      "sortParamsByRequiredFlag" : true,
      "groupId" : "org.openapitools",
      "invokerPackage" : "org.openapitools.client",
      "classVarName" : "default",
      "generateModelDocs" : true,
      "hasImport" : true,
      "generateModelTests" : true,
      "strictSpecBehavior" : true,
      "generateApiTests" : true,
      "classFilename" : "DefaultApi",
      "lambda" : {
        "lowercase" : { },
        "uppercase" : { },
        "titlecase" : { },
        "camelcase" : { },
        "indented" : { },
        "indented_8" : { },
        "indented_12" : { },
        "indented_16" : { }
      },
      "generateModels" : true,
      "infoUrl" : "https://openapi-generator.tech",
      "inputSpec" : "sample.yaml",
      "baseName" : "Default",
      "snippetlink" : { },
      "package" : "",
      "imports" : [ {
        "import" : "string",
        "classname" : "string"
      } ],
      "snippetinclude" : { },
      "contextPath" : "",
      "appDescription" : "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)",
      "releaseNote" : "Minor update",
      "version" : "1.0.0",
      "specinclude" : { },
      "headerAttributes" : true,
      "importPath" : ".Default",
      "licenseUrl" : "http://apache.org/licenses/LICENSE-2.0.html",
      "generatedYear" : "2020",
      "modelPackage" : "",
      "gitHost" : "github.com",
      "licenseInfo" : "All rights reserved",
      "hasModel" : false,
      "generateApis" : true,
      "basePathWithoutHost" : "",
      "operations" : {
        "classname" : "DefaultApi",
        "operation" : [ {
          "responseHeaders" : [ ],
          "hasAuthMethods" : false,
          "hasConsumes" : false,
          "hasProduces" : false,
          "hasParams" : true,
          "hasOptionalParams" : false,
          "hasRequiredParams" : true,
          "returnTypeIsPrimitive" : false,
          "returnSimpleType" : false,
          "subresourceOperation" : false,
          "isMapContainer" : false,
          "isListContainer" : false,
          "isMultipart" : false,
          "hasMore" : false,
          "isResponseBinary" : false,
          "isResponseFile" : false,
          "hasReference" : false,
          "isRestfulIndex" : false,
          "isRestfulShow" : false,
          "isRestfulCreate" : false,
          "isRestfulUpdate" : false,
          "isRestfulDestroy" : false,
          "isRestful" : false,
          "isDeprecated" : false,
          "isCallbackRequest" : false,
          "path" : "/test",
          "operationId" : "testPost",
          "httpMethod" : "POST",
          "summary" : "A simple endpoint.",
          "unescapedNotes" : "A more detailed explanation of our simple endpoint.",
          "notes" : "A more detailed explanation of our simple endpoint.",
          "baseName" : "Default",
          "servers" : [ ],
          "allParams" : [ {
            "isFormParam" : false,
            "isQueryParam" : false,
            "isPathParam" : false,
            "isHeaderParam" : true,
            "isCookieParam" : false,
            "isBodyParam" : false,
            "hasMore" : false,
            "isContainer" : false,
            "secondaryParam" : false,
            "isCollectionFormatMulti" : false,
            "isPrimitiveType" : true,
            "isModel" : false,
            "isExplode" : false,
            "baseName" : "X-Custom",
            "paramName" : "xCustom",
            "dataType" : "String",
            "dataFormat" : "fubar",
            "defaultValue" : "null",
            "style" : "simple",
            "example" : "xCustom_example",
            "jsonSchema" : "{\n  \"name\" : \"X-Custom\",\n  \"in\" : \"header\",\n  \"required\" : true,\n  \"style\" : \"simple\",\n  \"explode\" : false,\n  \"schema\" : {\n    \"$ref\" : \"#/components/schemas/Custom\"\n  }\n}",
            "isString" : true,
            "isNumeric" : false,
            "isInteger" : false,
            "isLong" : false,
            "isNumber" : false,
            "isFloat" : false,
            "isDouble" : false,
            "isByteArray" : false,
            "isBinary" : false,
            "isBoolean" : false,
            "isDate" : false,
            "isDateTime" : false,
            "isUuid" : false,
            "isUri" : false,
            "isEmail" : false,
            "isFreeFormObject" : false,
            "isListContainer" : false,
            "isMapContainer" : false,
            "isFile" : false,
            "isEnum" : false,
            "vendorExtensions" : { },
            "hasValidation" : true,
            "isNullable" : false,
            "required" : true,
            "exclusiveMaximum" : false,
            "exclusiveMinimum" : false,
            "pattern" : "/^(foo|bar|baz)+$/",
            "uniqueItems" : false
          } ],
          "bodyParams" : [ ],
          "pathParams" : [ ],
          "queryParams" : [ ],
          "headerParams" : [ {
            "isFormParam" : false,
            "isQueryParam" : false,
            "isPathParam" : false,
            "isHeaderParam" : true,
            "isCookieParam" : false,
            "isBodyParam" : false,
            "hasMore" : false,
            "isContainer" : false,
            "secondaryParam" : false,
            "isCollectionFormatMulti" : false,
            "isPrimitiveType" : true,
            "isModel" : false,
            "isExplode" : false,
            "baseName" : "X-Custom",
            "paramName" : "xCustom",
            "dataType" : "String",
            "dataFormat" : "fubar",
            "defaultValue" : "null",
            "style" : "simple",
            "example" : "xCustom_example",
            "jsonSchema" : "{\n  \"name\" : \"X-Custom\",\n  \"in\" : \"header\",\n  \"required\" : true,\n  \"style\" : \"simple\",\n  \"explode\" : false,\n  \"schema\" : {\n    \"$ref\" : \"#/components/schemas/Custom\"\n  }\n}",
            "isString" : true,
            "isNumeric" : false,
            "isInteger" : false,
            "isLong" : false,
            "isNumber" : false,
            "isFloat" : false,
            "isDouble" : false,
            "isByteArray" : false,
            "isBinary" : false,
            "isBoolean" : false,
            "isDate" : false,
            "isDateTime" : false,
            "isUuid" : false,
            "isUri" : false,
            "isEmail" : false,
            "isFreeFormObject" : false,
            "isListContainer" : false,
            "isMapContainer" : false,
            "isFile" : false,
            "isEnum" : false,
            "vendorExtensions" : { },
            "hasValidation" : true,
            "isNullable" : false,
            "required" : true,
            "exclusiveMaximum" : false,
            "exclusiveMinimum" : false,
            "pattern" : "/^(foo|bar|baz)+$/",
            "uniqueItems" : false
          } ],
          "formParams" : [ ],
          "cookieParams" : [ ],
          "requiredParams" : [ {
            "isFormParam" : false,
            "isQueryParam" : false,
            "isPathParam" : false,
            "isHeaderParam" : true,
            "isCookieParam" : false,
            "isBodyParam" : false,
            "hasMore" : false,
            "isContainer" : false,
            "secondaryParam" : false,
            "isCollectionFormatMulti" : false,
            "isPrimitiveType" : true,
            "isModel" : false,
            "isExplode" : false,
            "baseName" : "X-Custom",
            "paramName" : "xCustom",
            "dataType" : "String",
            "dataFormat" : "fubar",
            "defaultValue" : "null",
            "style" : "simple",
            "example" : "xCustom_example",
            "jsonSchema" : "{\n  \"name\" : \"X-Custom\",\n  \"in\" : \"header\",\n  \"required\" : true,\n  \"style\" : \"simple\",\n  \"explode\" : false,\n  \"schema\" : {\n    \"$ref\" : \"#/components/schemas/Custom\"\n  }\n}",
            "isString" : true,
            "isNumeric" : false,
            "isInteger" : false,
            "isLong" : false,
            "isNumber" : false,
            "isFloat" : false,
            "isDouble" : false,
            "isByteArray" : false,
            "isBinary" : false,
            "isBoolean" : false,
            "isDate" : false,
            "isDateTime" : false,
            "isUuid" : false,
            "isUri" : false,
            "isEmail" : false,
            "isFreeFormObject" : false,
            "isListContainer" : false,
            "isMapContainer" : false,
            "isFile" : false,
            "isEnum" : false,
            "vendorExtensions" : { },
            "hasValidation" : true,
            "isNullable" : false,
            "required" : true,
            "exclusiveMaximum" : false,
            "exclusiveMinimum" : false,
            "pattern" : "/^(foo|bar|baz)+$/",
            "uniqueItems" : false
          } ],
          "optionalParams" : [ ],
          "tags" : [ {
            "name" : "default"
          } ],
          "responses" : [ {
            "headers" : [ ],
            "code" : "200",
            "message" : "OK",
            "hasMore" : false,
            "hasHeaders" : false,
            "isString" : false,
            "isNumeric" : false,
            "isInteger" : false,
            "isLong" : false,
            "isNumber" : false,
            "isFloat" : false,
            "isDouble" : false,
            "isByteArray" : false,
            "isBoolean" : false,
            "isDate" : false,
            "isDateTime" : false,
            "isUuid" : false,
            "isEmail" : false,
            "isModel" : false,
            "isFreeFormObject" : false,
            "isDefault" : true,
            "simpleType" : true,
            "primitiveType" : true,
            "isMapContainer" : false,
            "isListContainer" : false,
            "isBinary" : false,
            "isFile" : false,
            "jsonSchema" : "{\n  \"description\" : \"OK\"\n}",
            "vendorExtensions" : { },
            "uniqueItems" : false,
            "exclusiveMinimum" : false,
            "exclusiveMaximum" : false,
            "wildcard" : false
          } ],
          "callbacks" : [ ],
          "imports" : [ "string" ],
          "vendorExtensions" : { },
          "nickname" : "testPost",
          "operationIdLowerCase" : "testpost",
          "operationIdCamelCase" : "TestPost",
          "operationIdSnakeCase" : "test_post",
          "hasExamples" : false,
          "hasFormParams" : false,
          "restfulShow" : false,
          "restfulIndex" : false,
          "restfulCreate" : false,
          "restfulUpdate" : false,
          "restfulDestroy" : false,
          "restful" : false,
          "hasBodyParam" : false,
          "hasPathParams" : false,
          "hasQueryParams" : false,
          "bodyAllowed" : true,
          "hasHeaderParams" : true,
          "hasCookieParams" : false,
          "hasResponseHeaders" : false
        } ],
        "pathPrefix" : "default"
      },
      "artifactId" : "openapi-client",
      "hideGenerationTimestamp" : true,
      "unescapedAppDescription" : "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)",
      "artifactVersion" : "1.0.0",
      "appName" : "Test",
      "generateApiDocs" : true,
      "generatorVersion" : "4.2.3",
      "apiPackage" : "",
      "basePath" : "http://localhost",
      "classname" : "DefaultApi",
      "gitRepoId" : "GIT_REPO_ID",
      "generatedDate" : "2020-03-26T00:16:46.255299+11:00[Australia/Melbourne]",
      "infoEmail" : "team@openapitools.org",
      "appDescriptionWithNewLines" : "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)",
      "gitUserId" : "GIT_USER_ID"
    } ]
  },
  "apiPackage" : "",
  "hasServers" : true,
  "basePath" : "http://localhost",
  "specinclude" : { },
  "gitRepoId" : "GIT_REPO_ID",
  "generatedDate" : "2020-03-26T00:16:46.255299+11:00[Australia/Melbourne]",
  "infoEmail" : "team@openapitools.org",
  "appDescriptionWithNewLines" : "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)",
  "headerAttributes" : true,
  "gitUserId" : "GIT_USER_ID"
}
Related issues/PRs
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Save the supplied OpenAPI declaration as sample.yaml and run the stated openapi-generator command with the asciidoc generator. Inspect the generated output and the AsciidocDocumentationCodegen entry point; done means the schema example value foofoobazbarfoo is retained instead of xCustom_example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
documentation, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.