OpenAPITools / OpenAPITools/openapi-generator

[Question] How to access jsonSchema elements from template

Open
#9,896 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Question...
Is there a syntax that allows accessing a generic JSON element in a variable(jsonSchema) ?

I'm generating rust code and are having problems accessing some values...
Given the following yaml:

components:
  schemas:
    formEditSettingsNewUserGroup: # $ref: '#/components/schemas/formEditSettingsNewUserGroup'
      description: Create a new user Group
      type: object
      properties:
          name_:          
            type: string
            nullable: false
            description: New Group name
            example: NewGroupName
          isadmingroup_:
            description: Is Admin Group [0 - No, 1 - yes]
            nullable: false
            type: string
            enum:
              - 0
              - 1
            default:  0
---------  template[model.mustache.mustache]: --------- 
...
impl Default for {{{classname}}} {
    fn default() -> Self {
        Self {
{{#vars}}/* {{{.}}} */
{{>partial_var_name_default}}
{{/vars}}
            _extra_fields: HashMap::new(),
            ..Default::default()
        }
    }
}
--------- template[partial_var_name_default.mustache]: --------- 
        {{{name}}} : {{#defaultValue}}{{^required}}Some({{/required}}{{#isString}}{{#isEnum}}{{{datatypeWithEnum}}}::default(){{/isEnum}}{{^isEnum}}String::from("{{defaultValue}}"){{/isEnum}}{{/isString}}{{^isString}}{{defaultValue}}{{/isString}}{{^required}}){{/required}},
        {{/defaultValue}}{{^defaultValue}}{{^required}}None{{/required}}{{#required}}{{#isString}}String::from(""){{/isString}}{{#isInteger}}-1{{/isInteger}}{{#isLong}}-1{{/isLong}}{{#isFloat}}f32::NAN{{/isFloat}}{{#isDouble}}f64::NAN{{/isDouble}}{{/required}},{{/defaultValue}}
---------

I'm trying to access the Default value, it workds great, except for enums...
the default value is: [defaultValue='Isadmingroup__0' ]
which is not correct. if anything, in rust it should be:
[defaultValue='Isadmingroup_::_0' ]

the only place is it the text value is in the jsonSchema
jsonSchema='{
"type" : "string",
"description" : "Is Admin Group [0 - No, 1 - yes]",
"nullable" : false,
"default" : "0",
"enum" : [ "0", "1" ]

is there some syntax that allows access to the JSON elements ?
I've tried all permutations...
{{#jsonSchema}}{{{.}}}{{/jsonSchema}} yeilds the above

{{#jsonSchema}}{{#default}}{{{.}}}{{/default}}{{/jsonSchema}} yeilds nothing

what is the correct syntax for accessing "jsonSchema.default"

Output of template element

/* CodegenProperty{openApiType='string', baseName='isadmingroup_', complexType='null', getter='getIsadmingroup', setter='setIsadmingroup', description='Is Admin Group [0 - No, 1 - yes]', dataType='String', datatypeWithEnum='Isadmingroup', dataFormat='null', name='isadmingroup_', min='null', max='null', defaultValue='Isadmingroup__0', defaultValueWithParam=' = data.isadmingroup_;', baseType='String', containerType='null', title='null', unescapedDescription='Is Admin Group [0 - No, 1 - yes]', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{
  "type" : "string",
  "description" : "Is Admin Group [0 - No, 1 - yes]",
  "nullable" : false,
  "default" : "0",
  "enum" : [ "0", "1" ]
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=true, isModel=false, isContainer=false, isString=true, isNumeric=false, isInteger=false, isLong=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=false, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=true, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=[0, 1], allowableValues={enumVars=[{name=_0, isString=true, value=0}, {name=_1, isString=true, value=1}], values=[0, 1]}, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='Isadmingroup', nameInSnakeCase='ISADMINGROUP', enumName='Isadmingroup', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false, getHasRequired=false} */
        isadmingroup_ : Some(Isadmingroup::default()),

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

Start with the Rust generator templates model.mustache.mustache and partial_var_name_default.mustache, then inspect how the jsonSchema and defaultValue fields are exposed to Mustache. Compare the template context with the generated Rust output shown in the issue. Done means documenting the supported syntax for accessing the schema default, or clarifying the required template/model change.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, rust
Domain
api, tooling
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.