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

Support customization of contentTypeOverride for Header ContentType #2519

Merged
merged 1 commit into from
Jun 9, 2021
Merged

Support customization of contentTypeOverride for Header ContentType #2519

merged 1 commit into from
Jun 9, 2021

Conversation

joviegas
Copy link
Contributor

@joviegas joviegas commented Jun 9, 2021

Description

  • Support customization of contentTypeOverride for Header ContentType

Motivation and Context

  • By default the ContentType Header for Json type is set to application/x-amz-json-1.1 for json protocol.
  • While wellarchitected accepted application/json ContentType Header.
  • These changes will add a customization for services which accept application/jsonContentType header.

Why is this change required? What problem does it solve?

  • Customization required for application/jsonContentType header. Else the API calls to Services which accept application/jsonContentType header will not be successful.

If it fixes an open issue, please link to the issue here : PR #2510

File Modification

JsonProtocolSpec.java :

contentType attribute is populated only if CustomizationConfig is set. If not then we look for metadata.getContentType() else we donot add any ContentType, in this case it uses from DefaultJsonContentTypeResolver.

AwsJsonProtocolMetadata

Added contentType which is used while we resolve the ContentType.

BaseAwsJsonProtocolFactory

The Factory method that creates Builders for AwsJsonProtocolFactory. ContentType is added in the attributes which will be set by codegen. Here we select from ContentType if the ContentType is set else we select the default one as we used to do before.

services/wellarchitected/src/main/resources/codegen-resources/customization.config

The Config file to set wellarchitected services ContentType as application/json

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-sync.java

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-async.java

Expected generated files when ContentType Customization is set.

Rest of the changes are for testing.

Testing

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed
  • A short description of the change has been added to the CHANGELOG
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

Copy link
Contributor

@millems millems left a comment

Choose a reason for hiding this comment

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

Can we add a comment to the customization to make it clear that it only works for JSON protocols?

@@ -242,6 +242,18 @@ public final SubclassT protocolVersion(String protocolVersion) {
return getSubclass();
}

/**
* ContentType of the client (By default it is application/x-amz-json-<JSON version>).
Copy link
Contributor

Choose a reason for hiding this comment

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

This default will change soon in the future (like, the next few months - there's a SEP on the way), and we'll probably forget to update this comment. Should we link to where the default is defined instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added link to code where we set the default value AWS_JSON = new DefaultJsonContentTypeResolver("application/x-amz-json-");

Comment on lines 36 to 37
return protocolMetadata.contentTypeOverride() != null ? protocolMetadata.contentTypeOverride()
: prefix + protocolMetadata.protocolVersion();
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like "overrides" being bound so late. It means we have to pipe a lot information through from the code generator, which adds a lot more cross-module API surface area. Can we just have this line be return protocolMetadata.contentType(); and have the code generator define the content-type that's used?

We can have the default value be "prefix + protocolMetadata.protocolVersion()", but do that upstream in the protocol factory. I'd honestly prefer it to be defaulted in the code generator, but that would break people who just upgrade the JSON protocol module without updating their service clients (which we try not to do outside of minor version bumps).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the logic where the check for customized ContentType is present is moved to protocol factory.

Comment on lines 107 to 113
MetadataConfig customServiceMetadata = model.getCustomizationConfig().getCustomServiceMetadata();
// Preference is given to CustomizationConfig
if (customServiceMetadata != null && customServiceMetadata.getContentType() != null) {
methodSpec.addCode(".contentTypeOverride($S)", customServiceMetadata.getContentType());
} else if (metadata.getContentType() != null) {
methodSpec.addCode(".contentTypeOverride($S)", metadata.getContentType());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a blocker, but this is easier for me to read:

String contentType = Optional.ofNullable(model.getCustomizationConfig().getCustomServiceMetadata())
                             .map(MetadataConfig::getContentType)
                             .orElse(metadata.getContentType()));
methodSpec.addCode(".contentTypeOverride($S)", contentType);

Comment on lines 253 to 256
public final SubclassT contentTypeOverride(String contentTypeOverride) {
protocolMetadata.contentTypeOverride(contentTypeOverride);
return getSubclass();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think in the future we will move contentType to be always controlled from the codegen, because we'll need to treat each protocol differently. Should we just name this "contentType" since in the future it won't really be an "override"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed all occurrences of contentTypeOverride to contentType.

@sonarcloud
Copy link

sonarcloud bot commented Jun 9, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@joviegas joviegas merged commit 140abda into aws:master Jun 9, 2021
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.

None yet

2 participants