datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Can't get Extra.forbid using the command line

Open Roobxyz opened this issue 1 year ago • 6 comments

Hello -- this forbid line (line 230) never gets hit and I think the default is Extra.ignore meaning there's no easy way to enforce strict type checking. The inline If statement just checks to see if additionalProperties or allow_extra_fields is set, but, if say addtionalProperties was set to {'extra':'Extra.forbid'} I think this line would set it back to Extra.allow.

https://github.com/koxudaxi/datamodel-code-generator/blob/97d4928782593efb1d439d91e7abbc70740b4ecd/datamodel_code_generator/model/pydantic/base_model.py#L224-L230

Roobxyz avatar Mar 06 '23 20:03 Roobxyz

@Roobxyz I'm sorry for my late reply. We can set additionalProperties: false to set extra = Extra.forbid Input: https://github.com/koxudaxi/datamodel-code-generator/blob/7947f4dbf3efd7cef9209fa72660fb0a036f21c1/tests/data/openapi/additional_properties.yaml#L139-L149

Output: https://github.com/koxudaxi/datamodel-code-generator/blob/7947f4dbf3efd7cef9209fa72660fb0a036f21c1/tests/data/expected/parser/openapi/openapi_parser_parse_additional_properties/with_import_format.py#L39-L44

koxudaxi avatar Apr 23 '23 15:04 koxudaxi

components:
  schemas:
    AuthorizationSchema:
      $ref: "otherservice.yaml#/components/schemas/AuthorizationSchema"

what if I'm referencing a schema, "AuthorizationSchema" for example, and I can't modify it directly since it's provided by others.

To get the extra = Extra.forbid, I have tried the method below, and it works for data model generator, but my openapi editor complains "Schema validation: Property 'additionalProperties' is not allowed".

components:
  schemas:
    AuthorizationSchema:
      $ref: "otherservice.yaml#/components/schemas/AuthorizationSchema"
      additionalProperties: false

another way is to write like this

components:
  schemas:
    ExtendedAuthorizationSchema:
      allOf:
        - $ref: "otherservice.yaml#/components/schemas/AuthorizationSchema"
        - type: object
          additionalProperties: false

it suits openapi editor, but not datamodel generator (no extra = Extra.forbid is generated)

What's the right way to do it?

Is there a way to generate data model with extra field of BaseConfig being set to Extra.forbid ? image

@koxudaxi

git-malu avatar May 19 '23 07:05 git-malu

Hi @koxudaxi, thank you for this excellent tool! I have the same issue. For me, it's not practical to update the OpenAPI schema because it is provided by an external API vendor.

I can potentially ask them to add additionalProperties: false to all the schema models, but wouldn't it be better to default to Extra.forbid even when this is not specified?

neelabhg avatar Jan 30 '24 09:01 neelabhg

@git-malu @neelabhg Sorry for the late reply.

but wouldn't it be better to default to Extra.forbid even when this is not specified?

I understand the situation. As usual, this project has a policy of avoiding changes to the behavior of the defaults, so let's add an option.

koxudaxi avatar Jan 30 '24 17:01 koxudaxi

I think this is broken if combined with allOf and $ref.

For example this schema will not set Extra.forbid:

MySchema:
  allOf:
    - $ref: "#/components/schemas/BaseProperties"
    - type: object
      required:
        - title
        - description
      properties:
        title:
          type: string
        description:
          type: string
      additionalProperties: false

I get this output:

class MySchema(BaseProperties):
    title: str
    description: str

colinbr96 avatar Mar 13 '24 21:03 colinbr96

@koxudaxi hello, thanks for generator. Now if I don't specify additionalProperties then will be generated pydantic model with default Extra option (Extra.ignore). This is good behaviour for my project. If pydantic changes this behavour, then accuision Extra.ignore will be broken.

May be add some option for codegen, like "--default-ignore-fields [bool]" or "--default-extra-fields [enum]"?

nanervax avatar Aug 07 '24 08:08 nanervax