- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 258
 
add discriminator property support #1154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            eli-bl
  wants to merge
  9
  commits into
  openapi-generators:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
benchling:discriminators
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      0496ff8
              
                add discriminator property support
              
              
                eli-bl b75adda
              
                simply the discriminator logic a bit
              
              
                eli-bl 2941451
              
                remove new category of tests for now
              
              
                eli-bl d189f77
              
                lint
              
              
                eli-bl de1ddf3
              
                lint
              
              
                eli-bl 9071de0
              
                handle a case where there's multiple values mapped to same type
              
              
                eli-bl 0a8b3f7
              
                documentation
              
              
                eli-bl f2e84aa
              
                Merge branch 'upstream-main' into discriminators
              
              
                eli-bl 8197b14
              
                lint
              
              
                eli-bl File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| --- | ||
| default: minor | ||
| --- | ||
| 
     | 
||
| # Add discriminator property support | ||
| 
     | 
||
| The optional `discriminator` field, when used in a schema with `anyOf` or `oneOf` as described in [OpenAPI 3.1.0](https://spec.openapis.org/oas/v3.1.0.html#discriminator-object), now correctly produces deserialization logic for using the specified property value to determine the appropriate type. | ||
| 
     | 
||
| In this example, `PolymorphicModel.thing` will be deserialized as a `ThingA` if the value of the `modelType` property is `"ThingA"`, or as a `ThingB` if the value is `"ThingB"`: | ||
| 
     | 
||
| ```yaml | ||
| ThingA: | ||
| type: object | ||
| properties: | ||
| thingType: | ||
| type: string | ||
| name: | ||
| type: string | ||
| 
     | 
||
| ThingB: | ||
| type: object | ||
| properties: | ||
| thingType: | ||
| type: string | ||
| name: | ||
| type: string | ||
| 
     | 
||
| PolymorphicModel: | ||
| type: object | ||
| properties: | ||
| thing: | ||
| anyOf: | ||
| - "#/components/schemas/ThingA" | ||
| - "#/components/schemas/ThingB" | ||
| discriminator: | ||
| propertyName: modelType | ||
| ``` | ||
| 
     | 
||
| If you want to use property values that are not the same as the schema names, you can add a `mapping`. In this example, the value is expected to be `"A"` or `"B"`, instead of `"ThingA"` or `"ThingB"`: | ||
| 
     | 
||
| ```yaml | ||
| discriminator: | ||
| propertyName: modelType | ||
| mapping: | ||
| A: "#/components/schemas/ThingA" | ||
| B: "#/components/schemas/ThingB" | ||
| ``` | ||
| 
     | 
||
| This could also be written more concisely as: | ||
| 
     | 
||
| ```yaml | ||
| discriminator: | ||
| propertyName: modelType | ||
| mapping: | ||
| A: "ThingA" | ||
| B: "ThingB" | ||
| ``` | ||
| 
     | 
||
| If you specify a property name that does not exist in all of the variant schemas, the behavior is undefined. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            18 changes: 9 additions & 9 deletions
          
          18 
        
  end_to_end_tests/golden-record/my_test_api_client/models/a_discriminated_union_type_1.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            18 changes: 9 additions & 9 deletions
          
          18 
        
  end_to_end_tests/golden-record/my_test_api_client/models/a_discriminated_union_type_2.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -32,6 +32,7 @@ class ModelProperty(PropertyProtocol): | |
| relative_imports: set[str] | None | ||
| lazy_imports: set[str] | None | ||
| additional_properties: Property | None | ||
| ref_path: ReferencePath | None = None | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.  | 
||
| _json_type_string: ClassVar[str] = "dict[str, Any]" | ||
| 
     | 
||
| template: ClassVar[str] = "model_property.py.jinja" | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary in order for the discriminator in the spec to really be valid: a discriminator property must be a required property in all of the variant schemas. My current implementation wouldn't actually catch a mistake like this, but I figured it was best to have the test spec be valid.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least, that's my reading of what OpenAPI says. I'm basing it on the statement "The expectation now is that a property with name petType MUST be present in the response payload, and the value will correspond to the name of a schema defined in the OAS document."
However, this is a case (one of many) of the OpenAPI specification using a mix of normative and non-normative language. The MUST seems unambiguous, but it's in a paragraph that's describing this specific example with types of pets, where the schemas do say the property is required. So do they mean a discriminator property must always be required, or just that that's the behavior this example is illustrating?
Logically I feel like it makes sense for it to be required, and if it isn't, then I'm not sure what the client behavior should be: (a) refuse to parse the object if the property is missing, or (b) just fall back to "try parsing it as each of these types until one of them works"? What I've implemented so far in the generated code is (a).