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

Rework using OpenAPIResolver to resolve $ref URIs #101

Merged
merged 4 commits into from
Apr 2, 2020
Merged
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
2 changes: 1 addition & 1 deletion tcases-openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.0.17</version>
<version>2.0.19</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;

import java.util.Objects;
import java.util.Optional;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -54,7 +53,10 @@ public static <T> T expectedValueOf( T value, String description, Object... desc
*/
public static Parameter resolveParameter( OpenAPI api, Parameter parameter)
{
return componentParameterRef( api, parameter.get$ref()).orElse( parameter);
return
Optional.ofNullable( parameter.get$ref())
.map( ref -> componentParameterRef( api, ref))
.orElse( parameter);
}

/**
Expand All @@ -63,9 +65,13 @@ public static Parameter resolveParameter( OpenAPI api, Parameter parameter)
public static Schema<?> resolveSchema( OpenAPI api, Schema<?> schema)
{
return
schema == null
? null
: resolveSchemaType( componentSchemaRef( api, schema.get$ref()).orElse( schema));
schema == null?
null :

resolveSchemaType(
Optional.ofNullable( schema.get$ref())
.map( ref -> componentSchemaRef( api, ref))
.orElse( schema));
}

/**
Expand Down Expand Up @@ -102,99 +108,93 @@ public static ComposedSchema resolveSchemaMembers( OpenAPI api, ComposedSchema c
*/
public static RequestBody resolveRequestBody( OpenAPI api, RequestBody requestBody)
{
return componentRequestBodyRef( api, requestBody.get$ref()).orElse( requestBody);
return
Optional.ofNullable( requestBody.get$ref())
.map( ref -> componentRequestBodyRef( api, ref))
.orElse( requestBody);
}

/**
* If the given response is defined by a reference, returns the referenced response. Otherwise, returns the given response.
*/
public static ApiResponse resolveResponse( OpenAPI api, ApiResponse response)
{
return componentResponseRef( api, response.get$ref()).orElse( response);
return
Optional.ofNullable( response.get$ref())
.map( ref -> componentResponseRef( api, ref))
.orElse( response);
}

/**
* If the given header is defined by a reference, returns the referenced header. Otherwise, returns the given header.
*/
public static Header resolveHeader( OpenAPI api, Header header)
{
return componentHeaderRef( api, header.get$ref()).orElse( header);
return
Optional.ofNullable( header.get$ref())
.map( ref -> componentHeaderRef( api, ref))
.orElse( header);
}

/**
* When the given reference is non-null, returns the component parameter referenced.
*/
public static Optional<Parameter> componentParameterRef( OpenAPI api, String reference)
public static Parameter componentParameterRef( OpenAPI api, String reference)
{
return
Optional.ofNullable( reference)

.map( ref -> componentName( COMPONENTS_PARAMETERS_REF, ref))
.filter( Objects::nonNull)

.map( name -> expectedValueOf( expectedValueOf( api.getComponents(), "Components").getParameters(), "Component parameters").get( name))
.filter( Objects::nonNull);
.flatMap( ref -> Optional.ofNullable( componentName( COMPONENTS_PARAMETERS_REF, ref)))
.flatMap( name -> Optional.ofNullable( expectedValueOf( expectedValueOf( api.getComponents(), "Components").getParameters(), "Component parameters").get( name)))
.orElseThrow( () -> new IllegalStateException( String.format( "Can't resolve parameter reference=%s", reference)));
}

/**
* When the given reference is non-null, returns the component schema referenced.
*/
@SuppressWarnings("rawtypes")
public static Optional<Schema> componentSchemaRef( OpenAPI api, String reference)
public static Schema componentSchemaRef( OpenAPI api, String reference)
{
return
Optional.ofNullable( reference)

.map( ref -> componentName( COMPONENTS_SCHEMAS_REF, ref))
.filter( Objects::nonNull)

.map( name -> expectedValueOf( expectedValueOf( api.getComponents(), "Components").getSchemas(), "Component schemas").get( name))
.filter( Objects::nonNull);
.flatMap( ref -> Optional.ofNullable( componentName( COMPONENTS_SCHEMAS_REF, ref)))
.flatMap( name -> Optional.ofNullable( expectedValueOf( expectedValueOf( api.getComponents(), "Components").getSchemas(), "Component schemas").get( name)))
.orElseThrow( () -> new IllegalStateException( String.format( "Can't resolve schema reference=%s", reference)));
}

/**
* When the given reference is non-null, returns the component request body referenced.
*/
public static Optional<RequestBody> componentRequestBodyRef( OpenAPI api, String reference)
public static RequestBody componentRequestBodyRef( OpenAPI api, String reference)
{
return
Optional.ofNullable( reference)

.map( ref -> componentName( COMPONENTS_REQUEST_BODIES_REF, ref))
.filter( Objects::nonNull)

.map( name -> expectedValueOf( expectedValueOf( api.getComponents(), "Components").getRequestBodies(), "Component request bodies").get( name))
.filter( Objects::nonNull);
.flatMap( ref -> Optional.ofNullable( componentName( COMPONENTS_REQUEST_BODIES_REF, ref)))
.flatMap( name -> Optional.ofNullable( expectedValueOf( expectedValueOf( api.getComponents(), "Components").getRequestBodies(), "Component request bodies").get( name)))
.orElseThrow( () -> new IllegalStateException( String.format( "Can't resolve request body reference=%s", reference)));
}

/**
* When the given reference is non-null, returns the component response referenced.
*/
public static Optional<ApiResponse> componentResponseRef( OpenAPI api, String reference)
public static ApiResponse componentResponseRef( OpenAPI api, String reference)
{
return
Optional.ofNullable( reference)

.map( ref -> componentName( COMPONENTS_RESPONSES_REF, ref))
.filter( Objects::nonNull)

.map( name -> expectedValueOf( expectedValueOf( api.getComponents(), "Components").getResponses(), "Component responses").get( name))
.filter( Objects::nonNull);
.flatMap( ref -> Optional.ofNullable( componentName( COMPONENTS_RESPONSES_REF, ref)))
.flatMap( name -> Optional.ofNullable( expectedValueOf( expectedValueOf( api.getComponents(), "Components").getResponses(), "Component responses").get( name)))
.orElseThrow( () -> new IllegalStateException( String.format( "Can't resolve response reference=%s", reference)));
}

/**
* When the given reference is non-null, returns the component header referenced.
*/
public static Optional<Header> componentHeaderRef( OpenAPI api, String reference)
public static Header componentHeaderRef( OpenAPI api, String reference)
{
return
Optional.ofNullable( reference)

.map( ref -> componentName( COMPONENTS_HEADERS_REF, ref))
.filter( Objects::nonNull)

.map( name -> expectedValueOf( expectedValueOf( api.getComponents(), "Components").getHeaders(), "Component headers").get( name))
.filter( Objects::nonNull);
.flatMap( ref -> Optional.ofNullable( componentName( COMPONENTS_HEADERS_REF, ref)))
.flatMap( name -> Optional.ofNullable( expectedValueOf( expectedValueOf( api.getComponents(), "Components").getHeaders(), "Component headers").get( name)))
.orElseThrow( () -> new IllegalStateException( String.format( "Can't resolve header reference=%s", reference)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.ObjectMapperFactory;
import io.swagger.v3.parser.OpenAPIResolver;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.util.OpenAPIDeserializer;
import io.swagger.v3.parser.util.ResolverFully;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -166,15 +169,27 @@ public OpenAPI read()
throw new OpenApiReaderException( getLocation(), e);
}

SwaggerParseResult parseResult= new OpenAPIDeserializer().deserialize( json, Objects.toString( getLocation(), null));
String location = Objects.toString( getLocation(), null);
SwaggerParseResult parseResult= new OpenAPIDeserializer().deserialize( json, location);

Optional.of( parseResult)
.map( SwaggerParseResult::getMessages)
.filter( messages -> !messages.isEmpty())
.map( messages -> new OpenApiReaderException( getLocation(), messages))
.ifPresent( failure -> { throw failure; });

return parseResult.getOpenAPI();
OpenAPI api = parseResult.getOpenAPI();
try
{
api = new OpenAPIResolver( api, null, location).resolve();
new ResolverFully( false).resolveFully( api);
}
catch( Exception e)
{
throw new OpenApiReaderException( getLocation(), e);
}

return api;
}

public void close()
Expand Down