Skip to content

Commit 97ae018

Browse files
committed
add OR union data provider
1 parent 337f72c commit 97ae018

File tree

7 files changed

+58
-2
lines changed

7 files changed

+58
-2
lines changed

example/java-jaxrs/src/main/java/com/example/resource/PetResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.example.data.PetData;
2323
import com.example.exception.BadRequestException;
2424
import com.example.exception.NotFoundException;
25+
import com.github.javafaker.Faker;
2526
import com.github.tminglei.bind.BindObject;
2627
import com.github.tminglei.bind.FormBinder;
2728
import com.github.tminglei.bind.Messages;
@@ -49,7 +50,7 @@ public class PetResource {
4950
static Mapping<?> petStatus = $(text(oneOf(Arrays.asList("available", "pending", "sold"))))
5051
.desc("pet status in the store").example("available").$$;
5152
static Mapping<?> pet = $(mapping(
52-
field("id", $(longv()).desc("pet id").example(gen("petId")).$$),
53+
field("id", $(longv()).desc("pet id").example(gen("petId").or(gen(() -> new Faker().number().randomNumber()))).$$),
5354
field("name", $(text(required())).desc("pet name").$$),
5455
field("category", $(mapping(
5556
field("id", longv(required())),

src/main/java/com/github/tminglei/swagger/fake/DataProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ default void setRequired(boolean required) {}
3131
*/
3232
Object get();
3333

34+
35+
///---
36+
37+
default DataProvider or(DataProvider other) {
38+
return new OrDataProvider(this, other);
39+
}
40+
3441
}

src/main/java/com/github/tminglei/swagger/fake/DataProviders.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static void setInstance(DataProviders instance) {
4343
/**
4444
* collect data providers from schema
4545
*
46+
* @param swagger the hosting swagger object
4647
* @param schema the schema object
4748
* @param clean whether to clean data providers in the schema
4849
* @return organized data provider

src/main/java/com/github/tminglei/swagger/fake/DataWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public interface DataWriter {
1414
* @param writer target writer
1515
* @param format target format
1616
* @param provider data provider
17+
* @throws IOException
1718
*/
1819
void write(Writer writer, String format, DataProvider provider) throws IOException;
1920

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.tminglei.swagger.fake;
2+
3+
import java.util.Map;
4+
import java.util.Optional;
5+
6+
/**
7+
* Created by minglei on 4/23/17.
8+
*/
9+
public class OrDataProvider implements DataProvider {
10+
private DataProvider first;
11+
private DataProvider second;
12+
13+
public OrDataProvider(DataProvider first, DataProvider second) {
14+
this.first = first;
15+
this.second = second;
16+
}
17+
18+
@Override
19+
public void setRequestParams(Map<String, String> params) {
20+
first.setRequestParams(params);
21+
second.setRequestParams(params);
22+
}
23+
24+
@Override
25+
public void setRequired(boolean required) {
26+
first.setRequired(required);
27+
second.setRequired(required);
28+
}
29+
30+
@Override
31+
public String name() {
32+
return first.name();
33+
}
34+
35+
@Override
36+
public Object get() {
37+
return Optional.ofNullable(first.get()).orElseGet(second::get);
38+
}
39+
}

src/main/java/com/github/tminglei/swagger/route/Route.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface Route {
2525
/**
2626
*
2727
* @param path request path, should match the binded path pattern
28-
* @return
28+
* @return params extracted from path
2929
*/
3030
Map<String, String> getPathParams(String path);
3131

src/main/java/com/github/tminglei/swagger/route/RouteImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public List<PathStaticElement> getStaticPathElements() {
141141
/**
142142
* Use of this method assumes the path given matches this Route.
143143
*
144+
* @param paramName param name
145+
* @param path path
144146
* @return the value of the named parameter in the path, or null if
145147
* no named parameter exists with the given name
146148
*/
@@ -159,6 +161,8 @@ public String getNamedParameter(String paramName, String path) {
159161
/**
160162
* Use of this method assumes the path given matches this Route.
161163
*
164+
* @param index the param index
165+
* @param path path
162166
* @return the value of the splat parameter at the given index,
163167
* or null if the splat parameter index does not exist
164168
*/
@@ -172,6 +176,9 @@ public String getSplatParameter(int index, String path) {
172176

173177
/**
174178
* Use of this method assumes the path given matches this Route.
179+
*
180+
* @param path path
181+
* @return splat params' values
175182
*/
176183
public String[] splat(String path) {
177184
List<PathSplatParamElement> splatParams = getSplatParameterElements();

0 commit comments

Comments
 (0)