Skip to content

Commit

Permalink
Allow disabling format on integers (#1904)
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase authored Aug 2, 2023
1 parent ea9544b commit 4009ad6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
42 changes: 42 additions & 0 deletions docs/source-2.0/guides/converting-to-openapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,48 @@ useIntegerType (``boolean``)
}
}
.. _generate-openapi-setting-disableIntegerFormat:

disableIntegerFormat (``boolean``)
Set to true to disable setting the ``format`` property when using the
"integer" type that is enabled by the :ref:`useIntegerType <generate-openapi-setting-useIntegerType>`
configuration setting.

.. code-block:: json
{
"version": "2.0",
"plugins": {
"openapi": {
"service": "example.weather#Weather",
"useIntegerType": true,
"disableIntegerFormat": true
}
}
}
With this enabled (the default), the ``format`` property is set to ``int32``
or ``int64`` for Integer or Long shapes respectively.

.. code-block:: json
{
"Foo": {
"type": "object",
"properties": {
"myInteger": {
"type": "integer",
"format": "int32"
},
"myLong": {
"type": "integer",
"format": "int64"
}
}
}
}
.. _generate-openapi-setting-onErrorStatusConflict:

onErrorStatusConflict (``String``)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
* Disables OpenAPI and JSON Schema features not supported by API Gateway.
*
* <p>API Gateway does not allow characters like "_". API Gateway
* doesn't support the "default" trait.
* doesn't support the "default" trait or `int32` or `int64` "format"
* values.
*/
final class AddDefaultConfigSettings implements ApiGatewayMapper {
@Override
Expand All @@ -36,5 +37,9 @@ public void updateDefaultSettings(Model model, OpenApiConfig config) {
config.setAlphanumericOnlyRefs(true);
config.getDisableFeatures().add("default");
config.setDisableDefaultValues(true);
// If the `useIntegerType` config has been set, this assures that
// `int32` and `int64` formats are not set on those integer types.
// See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html
config.setDisableIntegerFormat(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public enum HttpPrefixHeadersStrategy {
private Map<String, Node> jsonAdd = Collections.emptyMap();
private List<String> externalDocs = ListUtils.of(
"Homepage", "API Reference", "User Guide", "Developer Guide", "Reference", "Guide");
private boolean disableIntegerFormat = false;
private OpenApiVersion version = OpenApiVersion.VERSION_3_0_2;

public OpenApiConfig() {
Expand Down Expand Up @@ -321,6 +322,20 @@ public void setVersion(OpenApiVersion version) {
super.setJsonSchemaVersion(version.getJsonSchemaVersion());
}


public boolean getDisableIntegerFormat() {
return this.disableIntegerFormat;
}

/**
* Set to true to disable setting the `format` property on integer types.
*
* @param disableIntegerFormat True to disable setting format on integer types.
*/
public void setDisableIntegerFormat(boolean disableIntegerFormat) {
this.disableIntegerFormat = disableIntegerFormat;
}

/**
* Creates an OpenApiConfig from a Node value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public Schema.Builder updateSchema(Shape shape, Schema.Builder builder, JsonSche
}

boolean useOpenApiIntegerType = config instanceof OpenApiConfig
&& ((OpenApiConfig) config).getUseIntegerType();
&& ((OpenApiConfig) config).getUseIntegerType()
&& !((OpenApiConfig) config).getDisableIntegerFormat();

// Don't overwrite an existing format setting.
if (!builder.getFormat().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ public void supportsInt64() {
assertThat(document.getRootSchema().getFormat().get(), equalTo("int64"));
}

@Test
public void canDisableIntegerFormats() {
IntegerShape integerShape = IntegerShape.builder().id("a.b#C").build();
LongShape longShape = LongShape.builder().id("a.b#D").build();
Model model = Model.builder().addShapes(integerShape, longShape).build();
OpenApiConfig config = new OpenApiConfig();
config.setUseIntegerType(true);
config.setDisableIntegerFormat(true);
JsonSchemaConverter converter = JsonSchemaConverter.builder()
.addMapper(new OpenApiJsonSchemaMapper())
.config(config)
.model(model)
.build();

SchemaDocument integerDocument = converter.convertShape(integerShape);
SchemaDocument longDocument = converter.convertShape(longShape);

assertThat(integerDocument.getRootSchema().getFormat().isPresent(), equalTo(false));
assertThat(integerDocument.getRootSchema().getType().get(), equalTo("integer"));
assertThat(longDocument.getRootSchema().getFormat().isPresent(), equalTo(false));
assertThat(longDocument.getRootSchema().getType().get(), equalTo("integer"));
}

@Test
public void supportsFloatFormat() {
FloatShape shape = FloatShape.builder().id("a.b#C").build();
Expand Down

0 comments on commit 4009ad6

Please sign in to comment.