Skip to content
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

Use corresponding response schema if a discriminator mapping is present for requests _and_ responses #153

Merged
merged 1 commit into from
May 17, 2024

Conversation

praneetloke
Copy link
Member

When using a discriminator mapping for a request type, if the response schema also contains a discriminator mapping, then use the corresponding mapped schema.

// Don't prefix the parent name since this resource
// will already be scoped under a module.
discriminatedResourceName := ToPascalCase(discriminatedValue)
resourceTypeToken, err := o.gatherResourceProperties(discriminatedResourceName, *typeSchema.Value, resourceResponseType, apiPath, module)
if resourceResponseType != nil && resourceResponseType.Discriminator != nil {
responseSchemaRef := resourceResponseType.Discriminator.Mapping[discriminatedValue]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The discriminated values will be the same between the request and the response since the API can't possibly return a new type that it can't handle a request for. In other words, consider the following discriminator mapping in the request schema:

discriminator:
  propertyName: type
  mapping:
    "cat": "#/components/schemas/Cat"

The API can't return a dog type in the response, which wouldn't make sense.

@praneetloke praneetloke merged commit 1762749 into main May 17, 2024
1 check passed
@praneetloke praneetloke deleted the praneetloke/response-discriminator branch May 17, 2024 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant