openapi-codegen icon indicating copy to clipboard operation
openapi-codegen copied to clipboard

"required": true for a property. Does the codgen support it?

Open TemRhythm opened this issue 3 months ago • 0 comments

I have a Schema Object

{
  "Obj": {
    "description": "Obj",
    "required": [
      "id",
      "foo"
    ],
    "properties": {
      "id": {
        "type": "string",
        "pattern": "^[0-9a-f]{32}$"
      },
      "foo": {
        "type": "string",
        "pattern": "^[0-9a-f]{32}$"
      },
      "bar": {
        "type": "string",
        "pattern": "^[0-9a-f]{32}$"
      }
    }
  }
}

With this ShemaObject, a type generates perfectly fine.

/**
 * Obj
 */
export type Obj = {
  /**
   * @pattern ^[0-9a-f]{32}$
   */
  id: string;
  /**
   * @pattern ^[0-9a-f]{32}$
   */
  foo: string;
  /**
   * @pattern ^[0-9a-f]{32}$
   */
  bar?: string;
};

But if I move/distribute required to properties like this

{
  "Obj": {
    "description": "Obj",
    "properties": {
      "id": {
        "type": "string",
        "pattern": "^[0-9a-f]{32}$",
        "required": true
      },
      "foo": {
        "type": "string",
        "pattern": "^[0-9a-f]{32}$",
        "required": true
      },
      "bar": {
        "type": "string",
        "pattern": "^[0-9a-f]{32}$"
      }
    }
  }
}

It generates this:

/**
 * Obj
 */
export type Obj = {
  /**
   * @pattern ^[0-9a-f]{32}$
   */
  id?: string;
  /**
   * @pattern ^[0-9a-f]{32}$
   */
  foo?: string;
  /**
   * @pattern ^[0-9a-f]{32}$
   */
  bar?: string;
};

Conclusion: It ignores this approach of requiring fields if the required is set at the property level. Is it a bug? Is it possible to set up this approach?

TemRhythm avatar Oct 29 '25 16:10 TemRhythm