Skip to content

Commit 4091e00

Browse files
committed
Make generated OpenAPI request bodies required
1 parent 1374bbf commit 4091e00

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/openapi/AspectModelOpenApiGenerator.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ private void setResponseBodies( final Aspect aspect, final ObjectNode jsonNode,
248248
final ObjectNode componentsResponseNode = (ObjectNode) jsonNode.get( FIELD_COMPONENTS ).get( FIELD_RESPONSES );
249249
final ObjectNode referenceNode = FACTORY.objectNode()
250250
.put( REF, COMPONENTS_SCHEMAS + ( includePaging ? FIELD_PAGING_SCHEMA : aspect.getName() ) );
251-
final ObjectNode contentNode = getApplicationNode( referenceNode );
251+
final ObjectNode contentNode = getApplicationNode( referenceNode, false );
252252
componentsResponseNode.set( aspect.getName(), contentNode );
253253
contentNode.put( FIELD_DESCRIPTION, "The request was successful." );
254254
if ( !aspect.getOperations().isEmpty() ) {
255255
final ObjectNode operationResponseNode = FACTORY.objectNode()
256256
.put( REF, COMPONENTS_SCHEMAS + FIELD_OPERATION_RESPONSE );
257-
final ObjectNode wrappedOperationNode = getApplicationNode( operationResponseNode );
257+
final ObjectNode wrappedOperationNode = getApplicationNode( operationResponseNode, false );
258258
componentsResponseNode.set( FIELD_OPERATION_RESPONSE, wrappedOperationNode );
259259
wrappedOperationNode.put( FIELD_DESCRIPTION, "The request was successful." );
260260
}
@@ -265,21 +265,24 @@ private void setRequestBodies( final Aspect aspect, final OpenApiSchemaGeneratio
265265
final ObjectNode requestBodies = FACTORY.objectNode();
266266
componentNode.set( FIELD_REQUEST_BODIES, requestBodies );
267267
requestBodies.set( aspect.getName(),
268-
getApplicationNode( FACTORY.objectNode().put( REF, COMPONENTS_SCHEMAS + aspect.getName() ) ) );
268+
getApplicationNode( FACTORY.objectNode().put( REF, COMPONENTS_SCHEMAS + aspect.getName() ), true ) );
269269
if ( config.includeQueryApi() ) {
270270
requestBodies.set( FIELD_FILTER,
271-
getApplicationNode( FACTORY.objectNode().put( REF, COMPONENTS_SCHEMAS + FIELD_FILTER ) ) );
271+
getApplicationNode( FACTORY.objectNode().put( REF, COMPONENTS_SCHEMAS + FIELD_FILTER ), true ) );
272272
}
273273
if ( !aspect.getOperations().isEmpty() ) {
274274
requestBodies.set( FIELD_OPERATION,
275-
getApplicationNode( FACTORY.objectNode().put( REF, COMPONENTS_SCHEMAS + FIELD_OPERATION ) ) );
275+
getApplicationNode( FACTORY.objectNode().put( REF, COMPONENTS_SCHEMAS + FIELD_OPERATION ), true ) );
276276
}
277277
}
278278

279-
private ObjectNode getApplicationNode( final ObjectNode contentNode ) {
280-
return FACTORY.objectNode().set( FIELD_CONTENT,
281-
FACTORY.objectNode().set( APPLICATION_JSON,
282-
FACTORY.objectNode().set( FIELD_SCHEMA, contentNode ) ) );
279+
private ObjectNode getApplicationNode( final ObjectNode contentNode, final boolean addRequired ) {
280+
final ObjectNode result = FACTORY.objectNode();
281+
result.set( FIELD_CONTENT, FACTORY.objectNode().set( APPLICATION_JSON, FACTORY.objectNode().set( FIELD_SCHEMA, contentNode ) ) );
282+
if ( addRequired ) {
283+
result.put( FIELD_REQUIRED, true );
284+
}
285+
return result;
283286
}
284287

285288
private void setAspectSchemas( final Aspect aspect, final OpenApiSchemaGenerationConfig config, final ObjectNode jsonNode ) {
@@ -503,7 +506,10 @@ private ObjectNode getRequestEndpointsUpdate( final Aspect aspect, final ObjectN
503506
objectNode.set( "tags", FACTORY.arrayNode().add( aspect.getName() ) );
504507
objectNode.put( FIELD_OPERATION_ID, ( isPut ? FIELD_PUT : FIELD_PATCH ) + aspect.getName() );
505508
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, isEmpty( resourcePath ) ) );
506-
objectNode.set( FIELD_REQUEST_BODY, FACTORY.objectNode().put( REF, COMPONENTS_REQUESTS + aspect.getName() ) );
509+
final ObjectNode requestBody = FACTORY.objectNode();
510+
requestBody.put( FIELD_REQUIRED, true );
511+
requestBody.put( REF, COMPONENTS_REQUESTS + aspect.getName() );
512+
objectNode.set( FIELD_REQUEST_BODY, requestBody );
507513
objectNode.set( FIELD_RESPONSES, getResponsesForGet( aspect ) );
508514
return objectNode;
509515
}

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/openapi/AspectModelOpenApiGeneratorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ void testAspectWithPutOperation() {
586586
final SwaggerParseResult result = new OpenAPIParser().readContents( json.toString(), null, null );
587587
final OpenAPI openApi = result.getOpenAPI();
588588

589+
assertThat( openApi.getComponents().getRequestBodies().get( "AspectWithoutSeeAttribute" ).getRequired() ).isTrue();
590+
589591
final String apiEndpoint = "/{tenant-id}/aspect-without-see-attribute";
590592

591593
assertThat( openApi.getPaths().get( apiEndpoint ).getGet() ).isNotNull();

0 commit comments

Comments
 (0)