question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

oneOf is not treated the same as anyOf

See original GitHub issue

Describe the bug

The docs indicate that they are both treated as typing.Union

To Reproduce

Example schema:

components:
  schemas:
    Config:
      properties:
        setting:
          type: object
          additionalProperties:
            oneOf:  # anyOf
            - type: string
            - type: array
              items:
                type: string

Used commandline:

$ datamodel-codegen --input-file-type openapi --strip-default-none --input test.yaml --output model.py

oneOf:

from __future__ import annotations

from typing import Any, Dict, Optional

from pydantic import BaseModel, Extra


class Setting(BaseModel):
    class Config:
        extra = Extra.allow

    __root__: Any


class Config(BaseModel):
    setting: Optional[Dict[str, Setting]]

anyOf:

from __future__ import annotations

from typing import Dict, List, Optional, Union

from pydantic import BaseModel, Extra


class Setting(BaseModel):
    class Config:
        extra = Extra.allow

    __root__: Union[str, List[str]]


class Config(BaseModel):
    setting: Optional[Dict[str, Setting]]

Version:

  • OS: Windows 10
  • Python version: 3.8
  • datamodel-code-generator version: 0.7.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ofekcommented, Feb 16, 2021

Fixed!!! 🙇

0reactions
koxudaxicommented, Feb 16, 2021

@ofek I have released a fixed version 0.7.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

oneOf, anyOf, allOf, not - Swagger
Use the oneOf keyword to ensure the given data is valid against one of the specified ... Note the inline or referenced schema...
Read more >
Components are not referenced when combined with oneOf ...
When combining multiple schemas with oneOf, anyOf, allOf or not that where previously added to components, they will not get referenced.
Read more >
Best Match for Validation error with oneof or anyof
I am trying to get proper validation error from oneof or anyof pattern. I have json schema with two or more ...
Read more >
Frequently Asked Questions - Ajv JSON schema validator
Why don't "additionalProperties": false errors display the property name? Additional properties inside compound keywords anyOf, oneOf, etc. Why the keyword ...
Read more >
Understanding JSON Schema
JSON Schema is a powerful tool for validating the structure of JSON data. However, learning to use it by reading its.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found