Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ public JsonNode visitProperty( final Property property, final ObjectNode context
final Characteristic characteristic = determineCharacteristic( property );
final String referenceNodeName = getSchemaNameForModelElement( characteristic, property );
if ( processedProperties.contains( property ) ) {
return propertyNode.put( "$ref", "#/components/schemas/" + referenceNodeName );
return generateRefOrWrappedRef( propertyNode, referenceNodeName );
}
processedProperties.add( property );
final JsonNode schemaNode = characteristic.accept( this, context );
setNodeInRootSchema( schemaNode, referenceNodeName );
return propertyNode.put( "$ref", "#/components/schemas/" + referenceNodeName );
return generateRefOrWrappedRef( propertyNode, referenceNodeName );
}

@Override
Expand Down Expand Up @@ -652,4 +652,17 @@ private void addSammExtensionAttribute( final ObjectNode node, final ModelElemen
node.put( AspectModelJsonSchemaGenerator.SAMM_EXTENSION, describedElement.urn().toString() );
}
}

private ObjectNode generateRefOrWrappedRef( final ObjectNode propertyNode, final String referenceNodeName ) {
if ( !propertyNode.isEmpty() ) {
ArrayNode allOfArray = FACTORY.arrayNode();
ObjectNode refNode = FACTORY.objectNode();
refNode.put( "$ref", "#/components/schemas/" + referenceNodeName );
allOfArray.add( refNode );
propertyNode.set( "allOf", allOfArray );
return propertyNode;
} else {
return FACTORY.objectNode().put( "$ref", "#/components/schemas/" + referenceNodeName );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class AspectModelOpenApiGenerator extends JsonGenerator<OpenApiSchemaGene
private static final String FIELD_REQUEST_BODY = "requestBody";
private static final String FIELD_REQUIRED = "required";
private static final String FIELD_RESPONSES = "responses";
private static final String FIELD_ALL_OF = "allOf";
private static final String FIELD_SCHEMA = "schema";
private static final String FIELD_SCHEMAS = "schemas";
private static final String FIELD_STRING = "string";
Expand Down Expand Up @@ -508,7 +509,9 @@ private ObjectNode getRequestEndpointsUpdate( final Aspect aspect, final ObjectN
objectNode.set( FIELD_PARAMETERS, getRequiredParameters( parameterNode, isEmpty( resourcePath ) ) );
final ObjectNode requestBody = FACTORY.objectNode();
requestBody.put( FIELD_REQUIRED, true );
requestBody.put( REF, COMPONENTS_REQUESTS + aspect.getName() );

generateAllOfObjectForRef( requestBody, aspect.getName() );

objectNode.set( FIELD_REQUEST_BODY, requestBody );
objectNode.set( FIELD_RESPONSES, getResponsesForGet( aspect ) );
return objectNode;
Expand Down Expand Up @@ -593,6 +596,20 @@ private void handleRecursiveSchemas( final ObjectNode aspectSchema, final Object
}
}

private void generateAllOfObjectForRef( final ObjectNode requestBody, final String ref ) {
final ObjectNode contentNode = FACTORY.objectNode();
final ObjectNode appJsonNode = FACTORY.objectNode();
final ObjectNode schemaNode = FACTORY.objectNode();
final ArrayNode allOfArray = FACTORY.arrayNode();
final ObjectNode refNode = FACTORY.objectNode();
refNode.put( REF, COMPONENTS_REQUESTS + ref );
allOfArray.add( refNode );
schemaNode.set( FIELD_ALL_OF, allOfArray );
appJsonNode.set( FIELD_SCHEMA, schemaNode );
contentNode.set( APPLICATION_JSON, appJsonNode );
requestBody.set( FIELD_CONTENT, contentNode );
}

static final class ObjectNodeExtension {

static Function<ObjectNode, ObjectNode> getter( final String propName ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,16 @@ private void setSchemaInformation( final Aspect aspect, final PagingOption pagin
final ObjectNode node = (ObjectNode) getPathRootNode( pagingOption ).get( "response" );
schemaNode.set( AspectModelOpenApiGenerator.FIELD_PAGING_SCHEMA, node );

final ObjectNode itemNode = (ObjectNode) node.get( "properties" ).get( "items" );
itemNode.put( "$ref", "#/components/schemas/" + aspect.getName() );
final Property property = aspect.getProperties().get( 0 );
final String propertyName = property.getName();

final ObjectNode propertiesNode = (ObjectNode) node.get( "properties" );

final ObjectNode arrayNode = FACTORY.objectNode();
arrayNode.put( "type", "array" );
arrayNode.set( "items", FACTORY.objectNode().put( "$ref", "#/components/schemas/" + aspect.getName() ) );

propertiesNode.set( propertyName, arrayNode );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This logic fixing this structure:

"PagingSchema" : {
        "type" : "object",
        "properties" : {
          ...
          "items" : {
            "type" : "array",
            "items" : {
              "$ref" : "#/components/schemas/AspectWithErrorCollection"
            }
          }
        }
      },

to this:

"PagingSchema" : {
        "type" : "object",
        "properties" : {
          ...
          "aspectItems" : {
            "type" : "array",
            "items" : {
              "$ref" : "#/components/schemas/AspectWithErrorCollection"
            }
          }
        }
      },

It means, that attribute "items" will be taken from property name and not by default "items".

@atextor

}

private void validatePaging( final PagingOption definedPagingOption, final Set<PagingOption> possiblePagingOptions ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
"response": {
"type": "object",
"properties": {
"items": {
"type": "array"
},
"totalItems": {
"type": "number"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
"response": {
"type": "object",
"properties": {
"items": {
"type": "array"
},
"totalItems": {
"type": "number"
},
Expand Down
Loading
Loading