Forum Discussion

jaakkouu's avatar
jaakkouu
Occasional Visitor
4 years ago

OpenAPI how to create optional fields for typescript?

Hey, new to this kind of thing. I was wondering how generator handles optional fields?

 

My generator generates all models correctly. I've set them so that this particular model should have 3 properties and one of the should be required. Required field basically tells the user if response was successful. The other two are optional.

 

  • properties:
    • a:
      • type: boolean
    • b:
      • type: string
    • c:
      • type: string
  • required:
    • - a

I am using typescript as a flavor. After generating Java objects are generated, typescript models are created. Here is one of the functions that is created.

 

  • export function MyResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): MyResponse {
    • if ((json === undefined) || (json === null)) {
         return json;
      }
    • return {
        'a': json['a'],
        'b': !exists(json, 'b') ? undefined : json['b'],
        'c': !exists(json, 'c') ? undefined : json['c'],
      };}

 

 

This means that MyResponse will always have 3 fields which two of them might be set to undefined. What I would like to have are checks whether these properties exist. If they exist then they are added to the MyResponse object otherwise not. These fields are set to optional in FileUploadResponse interface which is great, but that function doesn't "care" about it. 

 

I don't want to create checks later on if such fields exists.

 

No RepliesBe the first to reply